Garbage Collection in the JVM

Garbage collection (GC) automatically reclaims heap memory occupied by objects that are no longer reachable. In Java, the JVM performs this work automatically, so application code normally does not explicitly free objects.

Java objects are typically allocated on the heap, and the garbage collector determines which objects are still reachable from GC roots such as active thread stacks, static references, and other runtime-managed references. An object that cannot be reached through these references is eligible for collection.

GC generally involves identifying live objects and reclaiming memory associated with objects that are no longer needed. Different JVM garbage collectors use different algorithms and trade-offs, so the exact collection behavior depends on the selected collector and JVM configuration.

A reference becoming `null` does not immediately free the referenced object. It only removes that particular path to the object; the object becomes collectible when no GC roots or other reachable objects can reach it.

Garbage collection reduces the need for manual memory management, but it is not free. Collection work consumes CPU resources and can introduce pauses or other latency effects. Java applications should therefore avoid unnecessary object creation and retain references only as long as needed.

Key points

Interview questions

What is garbage collection in Java?

Garbage collection is the JVM's automatic mechanism for reclaiming heap memory used by objects that are no longer reachable. The collector identifies objects that cannot be reached from GC roots and can reclaim their memory. This means Java code normally does not explicitly free ordinary objects.

When does an object become eligible for garbage collection?

An object becomes eligible when there is no longer a reachable reference path from any GC root to that object. For example, if a local variable is the only reference to an object and that variable is cleared, the object may become eligible. Eligibility means the collector may reclaim it; it does not guarantee immediate collection.

Does setting a Java reference to null immediately release the object's memory?

No. Setting the reference to `null` only removes that reference. If no other reachable reference points to the object, it becomes eligible for garbage collection, but the JVM decides when and whether to perform the relevant collection. Memory reclamation therefore should not be treated as an immediate consequence of assigning `null`.

Can a Java application have a memory leak even though it has garbage collection?

Yes. A Java memory leak can occur when an application unintentionally retains references to objects that it no longer needs. Because those objects remain reachable, the garbage collector considers them live and cannot reclaim their memory. Common causes include long-lived collections, caches without appropriate eviction, and listener registrations that are never removed.

Why can garbage collection affect application performance?

Garbage collection requires CPU and memory-management work, including identifying live objects and reclaiming or organizing heap space. Some collectors can introduce pauses or other latency effects, while different collectors make different throughput and latency trade-offs. Performance therefore depends on allocation patterns, heap configuration, collector choice, and application behavior.