When Is an Object Eligible for GC?
Fast recall
Quick revision
Core answer
Understanding of object reachability and the difference between eligibility and actual collection
Thinking that setting a reference to null immediately deletes the object — it only makes it eligible for GC
Explain GC roots and mention that circular references between unreachable objects are handled correctly by modern GC
Interview-ready guide
Interview answer
An object is eligible for garbage collection when no strong-reference path from a GC root reaches it.
Roots include references from active thread frames, static state reached through loaded classes, and JNI. If a method creates a Customer and neither returns it nor stores it in reachable state, the object may become eligible after its last useful reference disappears. Setting one variable to null removes only that path; another field, collection, thread, or native reference can keep the object live.
A cycle is not special: two objects that reference each other are still eligible if no root reaches the cycle. Eligibility does not mean immediate collection. A long-lived cache can therefore create a memory leak by retaining entries that the application no longer needs.
When Is an Object Eligible for GC
Direct Answer
An object is eligible for garbage collection when no chain of strong references from a GC root can reach it. Setting one variable to null or leaving a scope is enough only if no other reachable path still points to that object. Eligibility does not promise when—or whether before process exit—the JVM will reclaim it.
Code Example
public final class ReachabilityExample {
static Object retained;
static void references() {
Object first = new Object();
Object alias = first;
first = null; // the object is still reachable through alias
retained = alias;
alias = null; // the object is still reachable through retained
retained = null; // now it may be eligible if no other path exists
}
static void unreachableCycle() {
Node left = new Node();
Node right = new Node();
left.next = right;
right.next = left;
// If neither node escapes this call, the cycle can become unreachable.
}
static final class Node { Node next; }
}
These comments illustrate reference paths, not a guaranteed collection instant. The JIT may also prove that a local is no longer needed before the source scope ends.
Common Mistakes & Pitfalls
- Thinking null means instant deletion: Setting a variable to null removes one path. The object becomes eligible only when no strong-reference path from a GC root reaches it.
- Forgetting long-lived roots: A collection reachable through static state or a live thread can retain every object it contains until that path is cleared or its class loader or thread can end.
- Misunderstanding circular references: Two objects may point to each other and still be eligible when no root can reach either one. Reachability tracing handles such cycles.
Check your understanding
Answer these without looking back at the article.
- What is a GC root in Java?
- Does setting a variable to null immediately free the object?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.