Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are GC roots for classes?

In Java, there are special objects called Garbage Collection Roots (GC roots). They serve as a root objects for Garbage Collection marking mechanism (see picture).

enter image description here

This article describes four types of GC roots:

  • local variables
  • active threads
  • static variables
  • JNI references

It is also mentioned, that:

Classes themselves can be garbage-collected.

GC roots aren't collected thus classes themselves are not GC roots.

So what are GC roots for the classes?

like image 232
Kao Avatar asked Nov 28 '14 10:11

Kao


People also ask

What are GC roots?

Special objects called garbage-collection roots (GC roots; see Figure 2.2) are always reachable and so is any object that has a garbage-collection root at its own root. There are four kinds of GC roots in Java: Local variables are kept alive by the stack of a thread.

What is path to GC roots?

The path to the GC Roots shows the reference chain which prevents that the object is garbage collected. Objects decorated with a yellow dot are Garbage Collection (GC) Roots, i.e. objects which are assumed to be alive. Usually GC Roots are objects that are currently on the call stack of a thread or system classes.

What is GC root chain?

A local variable in a method that is currently running is considered to be a GC root. The objects referenced by these variables can always be accessed immediately by the method they are declared in, and so they must be kept around. The lifetime of these roots can depend on the way the program was built.

What is GC root in heap dump?

A garbage collection root is an object that is accessible from outside the heap. The following reasons make an object a GC root: System Class.


2 Answers

So what are GC roots for the classes?

Classloaders, effectively - via other GC roots.

If there is nothing which can reach a classloader - which means nothing can reach any classes created by that classloader or any instances of those classes - then both the classloader and the classes it created are eligible for garbage collection. Keeping them alive until then is necessary so that Class::forName and ClassLoader::findClass can be idempotent even when the class's static initializers are not.

Hidden classes (see https://openjdk.java.net/jeps/371) are exceptions to this rule. As an implementation detail of OpenJDK, so are the classes of method references, lambdas, and proxies created with the static methods of java.lang.reflect.Proxy. The classloader does not hold a strong reference to these classes.

like image 119
Jon Skeet Avatar answered Oct 02 '22 15:10

Jon Skeet


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

Memory Analyzer categorizes garbage collection roots according to the following list:

  1. Class loaded by system ClassLoader
    • static field in JDK classes(java.* etc)
  2. Live thread
    • stack -local vars, method params
    • java.lang.Thread instance
  3. Object held as synchronization monitor
  4. JNI references
  5. JVM specials...

Source 1 Source 2

like image 22
Premraj Avatar answered Oct 02 '22 16:10

Premraj