Sub topic 3
Reachability and GC Roots: When an Object Becomes Garbage
Prerequisites and the working model
This chapter assumes you understand Java classes, objects, references, and how to run a Java program from the command line.
The garbage collector (GC) does not ask whether an object is useful to your business logic. It checks whether the object is reachable. An object is reachable when the JVM can follow references from at least one special starting point called a GC root.
If no path exists from any GC root to an object, the object is unreachable. The garbage collector may then reclaim its memory.
Think of objects as nodes in a graph and references as arrows:
What counts as a GC root
A GC root is a reference the JVM treats as always available while it is active. Common roots include:
- Local variables and parameters in active stack frames.
- Static fields of loaded classes.
- Active Java threads and objects referenced by their thread stacks.
- References held by the JVM or native code through the Java Native Interface.
- Some JVM-internal references, such as class-loading and synchronization structures.
The exact root set depends on the running JVM and its implementation. The important rule is that collection starts by finding roots, then follows ordinary references from them.
A reference from one ordinary object to another does not make the first object a root. It only extends a reachable path that began at a root.
Following and breaking a path
The following program creates a small object graph:
public class ReachabilityDemo {
static class Node {
String name;
Node next;
Node(String name) {
this.name = name;
}
}
public static void main(String[] args) {
Node root = new Node("root");
root.next = new Node("child");
Node child = root.next;
child = null; // The child remains reachable through root.next.
root.next = null; // The child no longer has this path to a GC root.
root = null; // The root and its former child are now unreachable.
}
}While root points to the first object, the first object is reachable from the active main stack frame. Its next field gives the child object a reachable path too.
Setting child to null does not make the child unreachable because root.next still points to it. After root.next = null, the child has lost that path. After root = null, neither object has a path from the active stack frame.
The collector can reclaim unreachable objects. It does not need to reclaim them immediately, and Java provides no reliable way to force a collection.
Common reachability errors
A variable's scope is not exactly the same as its lifetime. A JIT compiler may recognize that a local variable will never be used again and stop treating it as live before the end of the method. Therefore, do not use source-code scope as a precise prediction of when collection can happen.
The reverse mistake is also common: developers remove a local reference but forget another path. A static field, a collection, a listener registration, a thread, or a cache may still point to the object. As long as one strong path from a GC root remains, the object remains reachable.
Reachability is transitive. If A is reachable and A points to B, then B is reachable. A cycle does not protect objects by itself. If two objects point to each other but no GC root points to either one, both objects are unreachable.
A practical checklist
When investigating an object that remains in memory, ask:
- Which GC root can reach the object?
- What reference path connects that root to the object?
- Is the path required, or should one link be removed?
- Is a static field, thread, listener, or collection holding the path?
- Could the object be part of an unreachable cycle instead?
Exercise
A local variable a points to object A. Object A points to object B. Another local variable b is set to null. Is object B necessarily unreachable?
Show answer
No. If a still points to A and A still points to B, B remains reachable through the path a -> A -> B.