Static variables are not garbage collected?




Contents of page >
  • Static variables/ fields are not garbage collected in java?
  • Can Static variables be garbage collected in some scenario?
  • But why Static variables are only garbage collected when the class loader which has loaded the class in which static field is there is garbage collected?


Static variables/ fields are not garbage collected in java?
Yes.

Can Static variables be garbage collected in some scenario?
Static variables are only garbage collected when the class loader which has loaded the class in which static field is there is garbage collected in java.


But why Static variables are only garbage collected when the class loader which has loaded the class in which static field is there is garbage collected?
Because static variables are referenced by the Class objects which are referenced by ClassLoaders. So, Static variables are only garbage collected when the class loader which has loaded the class in which static field is there is garbage collected in java.


Summary -
So in this tutorial we learned Static variables are not garbage collected in java.



Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

Related>

Acquiring lock on class, 2 Ways to acquire lock on class


Difference between object Lock and class Lock



 

Different type of garbage collectors in java>

>Serial collector / Serial GC (Garbage collector) in java

>Throughput GC (Garbage collector) or Parallel collector in java

>Concurrent Mark Sweep (CMS) collector / concurrent low pause garbage collector in java

>G1 garbage collector / Garbage first collector in java


JVM in detail - Garbage collection & heap structure >

>JVM Heap memory (Hotspot heap structure) with diagram in java


>What are Minor, Major and Full garbage collection in JVM in java



Important VM parameters >

>Most important and frequently used VM (JVM) PARAMETERS with examples in JVM Heap memory in java


Monitor, analyze garbage collection and fix MEMORY LEAK >

>How to monitor and analyze the garbage collection in 10 ways in java

>Detecting and fixing memory leak in java


eEdit
Must read for you :