Are static garbage collection fields open?

Given the hypothetical utility class, which is used only when setting up the program:

class MyUtils { private static MyObject myObject = new MyObject(); /*package*/static boolean doStuff(Params... params) { // do stuff with myObject and params... } } 

will myObject collect garbage when it is no longer in use, or will it depend on the life of the program?

+78
java garbage-collection static static-members
Jan 17 '09 at 9:02
source share
6 answers

Static variables cannot be selected for garbage collection during class loading. They can be collected when the corresponding class loader (which is responsible for loading this class) itself is collected for garbage.

See JLS Section 12.7 Unloading Classes and Interfaces

A class or interface can be unloaded if and only if its class-defining loader can be disposed of by the garbage collector [...] Classes and interfaces loaded by the boot loader may not be unloaded.

+98
Jan 17 '09 at 9:44
source share

Static variables are referenced by the class objects referenced by ClassLoaders - unless the ClassLoader somehow shakes Class (if possible), or ClassLoader itself becomes suitable for collecting (rather, think about unloading webapps) static variables (or, rather, objects that they reference) will not be collected.

+44
Jan 17 '09 at 9:33
source share

If you want the temporary object to be used for static initialization and then deleted, you can use a static initialization block, for example

 class MyUtils { static { MyObject myObject = new MyObject(); doStuff(myObject, params); } static boolean doStuff(MyObject myObject, Params... params) { // do stuff with myObject and params... } } 

since a static initializer block is a special type of static method, myObject is a local variable and can be garbage collected after the block completes.

+13
Jan 17 '09 at 18:49
source share

myObject is a reference , not an object . The object automatically collects garbage when there is no reference to it, because it is inaccessible.

Thus, the object behind the static link "myObject" can be garbage collected if you search for it with

 myObject = null; 

and there are no other references to this object.

However, static links and variables remain during the lifetime of your program.

+9
Jul 11 '14 at 12:49
source share

I think this answers your question - basically, unless the class comes from a special class loader and unloads the class.

+6
Jan 17 '09 at 9:12
source share

The key point here is the assembly of instances of the Garbage class, i.e. "Objects". The ClassLoader class is essentially an object. Therefore, if the Classloader object does not collect garbage, any references to them stored in the heap (i.e. static things) will almost never be garbage collected. The exception is String pool.

So, before you suddenly decide to make private static MyGiantClass myGiantObject = new MyGiantClass() Think twice, as I learned this hard way.

0
May 2 '18 at
source share



All Articles