Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When would the garbage collector erase an instance of an object that uses Singleton pattern?

When would the garbage collector erase an instance of an object that uses Singleton pattern?

Does an object hang around any longer than a regular object?

How can you manually force deletion/garbage collection of an object in Java?

Thanks.

like image 278
Julio Avatar asked Nov 08 '10 19:11

Julio


People also ask

When should a singleton be garbage collected?

No, a Singleton will not be garbage collected. Back in the old Java 1.1 days they could be, but now a Singleton will only be garbage collected when there are no references to its ClassLoader.

Can singleton class be garbage collected?

Singleton class has a static reference to the instantiated singleton object and hence it will never be garbage collected unless ofcourse as Jon Skeet as stated that the context that loaded this class (class loader) is itself eligible for garbage collection in which case that static reference will not longer be a GC ...

Which method is used by the garbage collector when deleting an instance?

Finalization. Finalization is performed by Garbage Collector just before destroying the objects. As a part of the finalization technique, the Garbage Collector calls the finalize() method on the object.

How do you destroy a singleton?

The true, non monobehaviour singleton can't really be destroyed or reinitialized as the static field is read only. If you need this you have to do the usual singleton approach with a normal private static variable. To force a reinitialization you just add a method "Destroy" which sets the static reference to "null".


2 Answers

There's a static reference to a singleton, so it won't be eligible for garbage collection until the classloader is eligible for garbage collection.

You can't force any object to be garbage collected; you can request that the garbage collector runs with System.gc() but it's only a request.

If you really want to make a "singleton" eligible for garbage collection, you'd probably want to have a method to set the static variable to null (and hope that nothing else had taken a copy of the reference). Obviously the next time anyone asked for an instance, it would need to be recreated... at which point it's not really a singleton, of course.

like image 76
Jon Skeet Avatar answered Sep 18 '22 22:09

Jon Skeet


There are special objects in Java called GC roots. They are always reachable and so are the objects that can be reached from these roots. These GC roots can never be garbage collected and so do the objects that are reachable from these roots. In Java static variables form GC roots.

Singleton class has a static reference to the instantiated singleton object and hence it will never be garbage collected unless ofcourse as Jon Skeet as stated that the context that loaded this class (class loader) is itself eligible for garbage collection in which case that static reference will not longer be a GC root.

Relevant answer here.

I think this was a bug prior to Java 1.2 when singleton instance could be garbage collected if there was no global reference to it but that was fixed in Java 1.2 and only way now it can be eligible for garbage collection is if the class loader that loaded this class was garbage collected.

like image 26
Aniket Thakur Avatar answered Sep 17 '22 22:09

Aniket Thakur