Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What are GC roots in the JVM? Name several kinds and explain how reachability decides what is garbage.

GC roots are the references the garbage collector treats as live by definition, without needing any other heap object to point to them: local variables and operand stack slots of active frames, static fields of loaded classes, JNI global references, live thread objects and objects used as held monitors. The collector marks everything it can reach from the roots, following references transitively, and everything left unmarked is garbage. Because Java traces from roots instead of counting references, unreachable cycles are collected too, and since finalization is deprecated for removal, objects should not depend on finalize to release resources.

Why this question is asked

Every memory leak in Java is, at bottom, an object that is still reachable from a GC root when we expected it not to be. Understanding roots is therefore the key to reading heap dumps and to explaining why some objects live and others die. Interviewers ask this to check the foundations under everything else we say about garbage collection. In this answer, we will see what reachability means, list the kinds of roots and what each one is, follow how marking works, look at why cycles are not a problem, and finish with why finalization should not be used.

1A Quick Recap: Live Means Reachable

The JVM does not know which objects our program will use again; it cannot predict the future. So it uses a safe approximation: an object is live if the program could still reach it by following references from somewhere the program certainly can access right now. Those starting points are the GC roots. Anything that cannot be reached from any root can never be used again, so its memory can be reused.

2The Kinds of GC Roots

The most common roots are local variables and operand stack slots in the frames of every running method, in every thread. If a method holds a reference in a local variable, that object is live while the method is running. Static fields of loaded classes are roots too: a static field belongs to its class, and a loaded class stays reachable, so whatever a static field points to stays live. JNI global references, created by native code to hold on to Java objects, are roots until the native code deletes them. Live Thread objects are roots, because a running thread is obviously in use. Objects whose monitors are currently held, because a thread is inside a synchronized block on them, are kept alive as well. The JVM also has internal roots, such as references held by the class loading system and by the JIT's compiled code.

Common GC roots
RootExampleKeeps alive until
Locals and operand stackList<Order> orders = load();The method's frame is popped
Static fieldsstatic Map<String, User> CACHEThe class is unloaded
JNI global referencesNewGlobalRef in native codeNative code calls DeleteGlobalRef
Live threadsA running Thread and its stackThe thread terminates
Held monitorssynchronized (lock) { ... }The lock is released

3How Marking Works

Tracing collectors start from the roots and follow every reference they find, marking each object they reach. When an object is marked, its own fields are scanned in turn, so marking spreads through the object graph until there is nothing new to visit. Everything that was marked is live; everything that was not is garbage. Different collectors do this at different times, during a pause or concurrently with the application, and young collections use extra tricks to avoid scanning the whole old generation, but the definition of live is the same.

Java
class Node { Node next; }
void demo() {
Node a = new Node(); // reachable: local variable a is a root
a.next = new Node(); // reachable through a
Node lonely = new Node();
lonely = null; // the third Node is now unreachable: garbage
} // after demo() returns, a's frame is gone: all three are garbage
Step throughMarking from the roots
  1. Collect the rootsStacks of all threads, statics, JNI globals, monitors
  2. Mark each object a root points toPut it on a work list
  3. Scan the fields of the next marked objectMark anything not yet marked
  4. Work list empty?Every reachable object visited
    YesMarking doneNoContinue scanning
  5. Reclaim unmarked objectsTheir memory is free for reuse

5 steps in order. Play them, or step through them one at a time.

4Why Cycles Are Collected

A simple reference-counting scheme frees an object when nothing points to it. It fails for cycles: two objects that point to each other always have a count of at least one, even when nothing else can reach them. Java's collectors do not count references; they trace from roots. A cycle that no root can reach is never marked, so it is collected like any other garbage.

Java
Node a = new Node();
Node b = new Node();
a.next = b;
b.next = a; // a cycle
a = null;
b = null; // no root reaches either node: both are collected at the next GC

5Why Not to Rely on finalize

Java used to let a class override finalize(), which the JVM would call some time after the object became unreachable. It was meant for releasing resources such as file handles, but it is unreliable: there is no guarantee when or even whether it runs, it slows down GC, and a finalizer can resurrect the object by storing this somewhere. JEP 421 deprecated finalization for removal in JDK 18.

Resources should be released explicitly with try-with-resources and close(). For a safety net when a caller forgets, java.lang.ref.Cleaner runs a cleanup action after an object becomes unreachable, without the problems of finalize. Being unreachable says nothing about when the GC will notice, so neither mechanism replaces explicit closing.

6Follow-up questions

Why do static fields act as roots only while their class loader is alive?

Because a static field belongs to a class, and a class can only be unloaded together with the class loader that loaded it. As long as that class loader is reachable, all its classes and their static fields stay live, so whatever the statics point to stays live. When the class loader and all its classes become unreachable, for example after an application is undeployed from a server, the GC can unload them, and the static fields stop being roots. Classes loaded by the system or application class loader are never unloaded in practice, which is why a static collection in application code effectively lives forever.

Is a local variable that is no longer used still a root?

Not necessarily. In compiled code, the JIT knows where each local is last used, and after that point the reference no longer keeps the object alive, even if the method has not returned. In the interpreter, the slot may keep the object alive until the frame is popped.

How do weak references fit into this?

A WeakReference does not count for reachability. An object reachable only through weak references is collected at the next GC that notices it, and the reference is cleared. Soft references are similar but are kept until the JVM needs memory.

7Conclusion

In this answer, we saw that GC roots are the references treated as live by definition: locals and operand stack slots of active frames, static fields of loaded classes, JNI global references, live threads and held monitors. The collector marks everything transitively reachable from them and reclaims the rest, so unreachable cycles are collected because Java traces instead of counting references. Finalization is deprecated for removal, and resources should be released explicitly, with Cleaner only as a safety net.

What to say in the interviewAbout 90 seconds aloud

Open with

GC roots are the references the garbage collector treats as live without any other heap object pointing to them; they are where marking starts. The main ones are local variables and operand stack slots in the active frames of every thread, static fields of loaded classes, JNI global references held by native code, live Thread objects, and objects whose monitors are currently held.

Then, if they want more

There are also JVM-internal roots like references from compiled code and the class loading system. Reachability then decides what is garbage: the collector marks every object a root points to, scans those objects' fields and marks what they point to, and continues until nothing new is found.

Anything left unmarked can never be used again by the program, so its memory is reclaimed. Because this is tracing rather than reference counting, cycles are not a problem: two objects pointing to each other but unreachable from any root are simply never marked.

One related point: finalization is deprecated for removal since JEP 421 in JDK 18, because there is no guarantee when a finalizer runs, it slows GC and can resurrect objects. So resources should be released explicitly with try-with-resources, and Cleaner can serve as a safety net.

And in practice, every Java memory leak is an object still reachable from a root, often a static field.

About this answer

Last updated
12 November 2026
Versions it names
Java 18
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What are GC roots in the JVM? Name several kinds and explain how reachability decides...": https://scrivio-interview-library.netlify.app/java/garbage-collection/0f1baa48b8/

More in Garbage collection