Garbage Collection in Java
In this lesson: How Java automatically frees memory — heap, eligibility, System.gc(), generations — explained simply. In Hindi & English.
What is garbage collection
Garbage collection is Java automatically freeing memory that objects no longer need. In languages like C you free memory by hand, which is error-prone. In Java, the JVM's garbage collector finds objects nobody uses anymore and reclaims their memory for you.
Objects live on the heap
Every object you create with new lives on the heap — a region of memory the JVM manages. As your program runs, it makes many objects there. The garbage collector works on the heap, cleaning up objects that are no longer reachable to keep memory available.
When an object is collected
An object becomes eligible for collection when nothing references it anymore:
- Its variable is set to
null. - The variable goes out of scope (a method ends).
- It is replaced by another object.
Once unreachable, the garbage collector may reclaim its memory — you do not control exactly when.
A simple example
String s = new String("Hello"); // object on the heap
s = null; // now unreachable
// the old "Hello" object is eligible for GC
Setting s = null removes the only reference to the "Hello" object, so nothing points to it. It is now eligible for garbage collection, and the JVM will free it when it runs the collector.
System.gc and finalize
System.gc(); // a SUGGESTION to run GC, not a command
System.gc() only asks the JVM to run garbage collection; the JVM decides whether and when. The old finalize() method (called before an object was collected) is deprecated — do not use it.
Why it helps
- Fewer bugs — no forgetting to free memory, no double-frees.
- No manual work — you focus on logic, not memory bookkeeping.
- Safer programs — a big reason Java is reliable for large systems.
The trade-off is less precise control over exactly when memory is freed, which is fine for most programs.
Common mistakes
- Thinking you must free memory. Java does it automatically.
- Relying on
System.gc(). It is only a suggestion, not a guarantee. - Using
finalize(). It is deprecated and unreliable. - Keeping unwanted references. Objects still referenced are never collected — a memory leak.
Summary
- Garbage collection frees memory of unused objects automatically.
- Objects live on the heap, managed by the JVM.
- An object is eligible when nothing references it anymore.
System.gc()is only a suggestion;finalize()is deprecated.- It reduces memory bugs — just drop references you no longer need.
Next, learn cleanup keywords in final, finally and finalize, or revise objects in classes and objects.
Frequently Asked Questions
What is garbage collection in Java?
When does an object become eligible for garbage collection?
Where do Java objects live in memory?
Can I force garbage collection in Java?
System.gc(), but it is only a request — the JVM decides when to actually run. In practice you rarely call it; the garbage collector runs automatically as needed.