Object Lifecycle & Garbage Collection
Object Lifecycle & Garbage Collection
Every object in Java is born, lives in memory for a while, and eventually dies. Unlike languages such as C or C++, you never have to manually free memory in Java — the garbage collector (GC) handles that automatically. Understanding when and why an object is removed helps you write code that is both correct and memory-efficient.
Step 1 — Object Creation
An object is created with the new keyword. Java does two things at that moment: it allocates a chunk of memory on the heap to hold the object's fields, and it runs the constructor to initialise those fields.
After this line, rex is a reference variable. It does not contain the object itself — it contains the memory address where the object lives. Think of it as a remote control: the remote is the variable, the TV is the object on the heap.
Step 2 — References Keep Objects Alive
An object stays alive as long as at least one reference points to it. You can have multiple references to the same object:
Step 3 — Becoming Eligible for Garbage Collection
An object becomes eligible for garbage collection (eligible for GC) the moment no reachable reference points to it anymore. There are several ways this happens:
- The variable goes out of scope. Local variables are destroyed when the method they belong to returns.
- You reassign the variable. The old object loses the reference.
- You set the variable to
null. Explicitly removes the reference.
null when you are done with them if they are stored in long-lived variables (such as static fields or instance fields of a long-running object). It is not needed for local variables because they disappear when the method returns anyway.
Step 4 — The Garbage Collector Runs
Being eligible for GC does not mean the object is freed immediately. The JVM's garbage collector runs on its own schedule — typically when heap memory runs low. It traces all reachable objects from a set of root references (local variables in running threads, static fields) and reclaims everything it cannot reach.
You can suggest that the GC run with System.gc(), but the JVM is free to ignore that hint. Never rely on it.
try-with-resources block.
Island of Isolation
Objects can make each other ineligible for GC even when no external code can reach them. This is called an island of isolation. The JVM's GC handles this correctly by using reachability from root references, not just reference counts.
The Object Lifecycle — Summary
- Created:
newallocates heap memory and runs the constructor. - In use: at least one reachable reference keeps it alive.
- Eligible for GC: no reachable reference exists.
- Reclaimed: GC frees the memory at a time it chooses.
free() in Java. Java was designed so developers focus on business logic, not memory management. The GC is sophisticated — modern collectors (G1, ZGC, Shenandoah) can reclaim memory with very short pauses even in large applications.
Practical Takeaway
For most everyday Java code you do not need to think about GC at all. Just let objects go out of scope naturally. The cases where it matters are: very large objects held in long-lived variables, cached collections that grow without bound, or classes that wrap native resources. In those cases, release references explicitly and use try-with-resources for anything closeable.