What are the GC roots for classes?

In Java, there are special objects called Roots Collection Roots (GC roots). They serve as root objects for the garbage labeling mechanism (see. Figure).

enter image description here

This article describes four types of GC roots:

  • local variables
  • active threads
  • static variables
  • JNI Links

It is also mentioned that:

classes themselves can be garbage collected.

GC roots are not collected, so the classes themselves are not GC roots.

So what are the roots of GC for classes?

+17
source share
2 answers

So what are the roots of GC for classes?

Classloaders, effectively - through other GC roots.

If there is nothing that can reach the class loader, which means that nothing can reach any class instances created by this class loader, then both the class loader and the created classes have the right to garbage collection.

+15
source

A garbage collection root is an object that is accessible from outside the heap.

Memory Analyzer classifies the roots of garbage collection according to the following list:

  1. The class is loaded by the ClassLoader system
    • static field in JDK classes (java. * etc.)
  2. Living thread
    • stack -local variables, method parameters
    • java.lang.Thread instance
  3. The object is held as a synchronization monitor.
  4. Jni links
  5. JVM special ...

Source 1 Source 2

+4
source

Source: https://habr.com/ru/post/1207879/


All Articles