Are static fields open for garbage collection?

JavaStaticGarbage CollectionStatic Members

Java Problem Overview


Given an hypothetical utility class that is used only in program setup:

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

will myObject be garbage collected when it is no longer being used, or will it stick around for the life of the program?

Java Solutions


Solution 1 - Java

Static variables cannot be elected for garbage collection while the class is loaded. They can be collected when the respective class loader (that was responsible for loading this class) is itself collected for garbage.

Check out the JLS Section 12.7 Unloading of Classes and Interfaces

> A class or interface may be unloaded > if and only if its defining class > loader may be reclaimed by the garbage > collector [...] Classes and interfaces > loaded by the bootstrap loader may not > be unloaded.

Solution 2 - Java

Static variables are referenced by Class objects which are referenced by ClassLoaders -so unless either the ClassLoader drops the Class somehow (if that's even possible) or the ClassLoader itself becomes eligible for collection (more likely - think of unloading webapps) the static variables (or rather, the objects they reference) won't be collected.

Solution 3 - Java

myObject is a reference and not an object. An object is automatically garbage collected when no reference points to it because it is unreachable.

So also the object behind a static reference "myObject" can be garbage collected if you dereference it with

myObject = null;

and there are no other references to this object.

However static references and variables remain for the lifetime of your program.

Solution 4 - Java

If you want a temporary object to be used for static initialisation then disposed of, you can use a static initialiser block, e.g.

class MyUtils {
   static
   {
      MyObject myObject = new MyObject();
      doStuff(myObject, params);
   }

   static boolean doStuff(MyObject myObject, Params... params) {
       // do stuff with myObject and params...
   }
}

since the static initialiser block is a special kind of static method, myObject is a local variable and can be garbage collected after the block finishes executing.

Solution 5 - Java

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

Solution 6 - Java

The key here is the Garbage Collection of Class instances i.e. Objects. ClassLoader instance is, in essence, an Object. So if the Classloader object is not garbage collected, any references of them stored in heap (i.e. static stuff) will almost never be garbage collected. The exception is String pool.

So before you suddenly decide to do private static MyGiantClass myGiantObject = new MyGiantClass() Think twice as I have learnt the hard way.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichael DeardeuffView Question on Stackoverflow
Solution 1 - Javabruno condeView Answer on Stackoverflow
Solution 2 - JavaJon SkeetView Answer on Stackoverflow
Solution 3 - JavaFelix KeilView Answer on Stackoverflow
Solution 4 - JavafinnwView Answer on Stackoverflow
Solution 5 - JavaTomView Answer on Stackoverflow
Solution 6 - Javaha9u63arView Answer on Stackoverflow