What is GC Root

First of all, we know the marking algorithm, the JVM’s marking algorithm can be understood as a reachability algorithm, so all reachability algorithms will have a starting point, and this starting point is the GC Root.

That is, it is necessary to find all living objects through the GC Root, and then all the remaining unmarked objects are the objects to be recycled.

image-20200406124709630

Characteristics of GC Root

  • Objects that are alive at the current moment!

Where is the GC Root

  • References to objects in the GC heap in the currently active stack frames of all Java threads; in other words, the reference type parameters/local variables/temporary values of all currently being called methods.
  • References to objects in the GC heap in some static data structures of the VM, for example, there are many such references in the Universe in the HotSpot VM.

Here is a question? Why do we need to set the GC root as the reference to the object in the GC heap in the currently active stack frame of all Java threads?

The reason is simple, the GC Root needs to ensure that the objects referred to are alive, and the objects in the current thread frame are alive at this moment.