0

I'am searching how jvm and garbage colelction work in java application and i would know if java singleton objects are eligible to java garbage colelction, how static fields in a class get garbage collected?
I know also that now in java8 and newest versions the metadata of objects is stored in metaspace, but what really contains this metadata.
Thanks.

abdelmouheimen
  • 136
  • 2
  • 16
  • You have a few questions wrapped up here – re: "metadata of objects", take a look at [_Heap and Non-Heap Memory_](https://docs.oracle.com/javase/7/docs/technotes/guides/management/jconsole.html) or other SO questions+answers [like this one](https://stackoverflow.com/questions/6091615/difference-between-on-heap-and-off-heap). – Kaan Dec 23 '19 at 19:36

1 Answers1

0

Static variables are garbage collected only once class loader that loaded class that this static variable is member of is garbage collected as well.

All class loaders except bootstrap class loader can be garbage collected, though this would rarely occur in most standard applications, since class loader will only be garbage collected if:

  • There is no reachable instance of any class loaded by class loader
  • There is no reachable class (including class that our static variable is part of) that is loaded by class loader
  • Class loader itself is not reachable

Note that this is simple explanation, there is more details if you are interested, good starting point would be Java 13 class unloading specification

FilipRistic
  • 2,661
  • 4
  • 22
  • 31