GC Roots

GC roots are runtime references from which a Java garbage collector begins tracing object reachability. Objects reachable from these roots are treated as live, while unreachable objects can be reclaimed.

A GC root is a reference that the garbage collector treats as a starting point for reachability analysis. The collector follows references from the roots through the object graph; an object is live if there is a reference path from at least one root to that object.

Typical root categories include references held by active Java thread stacks, loaded classes and their relevant static fields, and references maintained through JNI. JVM implementations can have additional root categories, so the exact set is implementation-dependent.

Consider a local variable that references an object. While the variable belongs to an active stack frame, the referenced object may be reachable from a GC root. If the variable goes out of scope or the frame is no longer active and no other root can reach the object, that object may become eligible for garbage collection.

GC roots are therefore different from ordinary objects in the heap. A normal heap object becomes reachable because another reachable object points to it; roots provide the starting points for the traversal rather than requiring another heap object to reference them.

Key points

Interview questions

What is a GC root in Java?

A GC root is a reference that the garbage collector uses as a starting point when determining which objects are reachable. The collector traces references outward from these roots through the heap. Objects that cannot be reached from any root are eligible for reclamation. Common examples include references in active thread stacks and static fields.

Why can a local variable keep an object alive?

A local variable in an active method can hold a reference from a thread's stack frame. The garbage collector can treat that stack-held reference as a starting point for reachability. Therefore, the referenced object and objects reachable from it may remain live. Once the reference is no longer considered live and no other root reaches the object, it can become eligible for collection.

Can a static field prevent an object from being garbage collected?

Yes. A static field associated with a loaded class can provide a root path to the object it references. As long as that reference remains reachable through the relevant class and no other conditions remove the root path, the referenced object can remain live. This is one reason unintended static references can contribute to memory retention.

What is the difference between an object being unreachable and being immediately garbage collected?

Unreachable means there is no path from any applicable GC root to the object. That makes the object eligible for reclamation, but it does not mean the JVM must reclaim it immediately. Garbage collection is performed according to the JVM's collector and runtime conditions. Therefore, code should not rely on a particular collection time after an object becomes unreachable.

Why do GC roots matter when diagnosing a memory leak?

A memory-retention problem can often be understood by finding why supposedly disposable objects remain reachable. Heap-analysis tools can show a retained object and the reference path leading back toward a GC root. The first unexpected reference on that path often identifies the component retaining the object. This makes GC-root paths useful for distinguishing genuine allocation pressure from objects that are unintentionally kept alive.