Object Lifecycle & Garbage Collection Eligibility
1Concept
An object in the Heap is eligible for Garbage Collection as soon as it becomes unreachable from any live thread GC Root. References can be nullified, reallocated, or left in an 'Island of Isolation' (where objects reference each other but have no path to a live thread).
2Architecture Diagram
[ GC Root: Live Thread Stack Frame ]
|
v
[ Object A ] ----> [ Object B ] (Both Reachable: Kept in Heap!)
[ Object C ] <---> [ Object D ] (Island of Isolation: Unreachable! Eligible for GC)3Code Example
Core Java
public class GcEligibilityDemo {
static class Node {
String name;
Node next;
Node(String name) { this.name = name; }
}
public static void main(String[] args) {
Node n1 = new Node("Node1");
Node n2 = new Node("Node2");
n1.next = n2;
n2.next = n1; // Mutual reference (Circular link)
// Severing GC Root references
n1 = null;
n2 = null;
System.out.println("References nullified. Circular island of isolation created.");
System.out.println("Both Node1 and Node2 are now 100% eligible for Garbage Collection.");
}
}4Expected Output
References nullified. Circular island of isolation created. Both Node1 and Node2 are now 100% eligible for Garbage Collection.
5Key Takeaways
- ✓Modern JVM GC algorithms use tracing collectors, easily reclaiming circular islands of isolation.
- ✓`System.gc()` is merely a hint to the JVM; it does NOT guarantee immediate collection.
- ✓Never rely on `finalize()` for resource cleanup (deprecated since Java 9, removed in modern Java).