Nearby lessons
104 of 125Java - Garbage Collection
- What garbage collection is and why Java needs it
- When an object becomes eligible for collection
- How to request garbage collection: System.gc()
- finalize() vs the modern Cleaner
- The Generational idea (new vs old memory)
Garbage Collection is a core concept of the Java language. This lesson explains The Problem Garbage Collection Solves, When Does an Object Become Garbage? and Interesting Case — Circular References with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
The Problem Garbage Collection Solves
In languages like C/C++, the programmer must manually free memory after use (free in C, delete in C++). If you forget, the program slowly eats memory and crashes — this is called a memory leak.
Java removes this headache with the Garbage Collector (GC) — an automatic process that finds unused objects and frees their memory while the program runs. You never have to free memory yourself.
Think of the GC as a housekeeping staff: whenever you leave an object no longer needed, the GC quietly throws it out and clears the space. That is why Java is called a robust and developer-friendly language.
When Does an Object Become Garbage?
An object becomes eligible for garbage collection when no reference points to it anymore — in other words, nothing can ever use it again. This happens in two common ways:
Interesting Case — Circular References
Here is a subtle point: if objects refer to each other in a cycle but nothing outside refers to them, they are still garbage. The GC understands that the whole cycle is unreachable.
How to Request Garbage Collection
You cannot force the GC to run instantly — it decides when to run. But you can request it politely with:
finalize() vs the Modern Cleaner
Older Java had finalize() — a method the GC called just before destroying an object, letting you clean up resources. But finalize() was unreliable and slow.
Updated knowledge: finalize() is deprecated (since Java 9) and removed (Java 18). The modern replacement is the Cleaner mechanism (Java 9+) or simply try-with-resources. Use these instead:
finalize() vs the Modern Cleaner
Memory and the Heap
Objects live in the heap — a big shared memory area. We can peek at memory usage using the Runtime class:
The Generational Idea (How Modern GC Works)
Modern JVMs use a smart trick called generational collection. Most objects die young, so memory is split into areas:
| Area | What lives there | Why |
|---|---|---|
| Young Generation | Newly created objects | Most objects die here quickly — collecting here is cheap and frequent. |
| Old Generation | Objects that survived many collections | Long-lived objects move here; collected rarely. |
| (Meta/Method area) | Class definitions and static data | Not part of normal object GC. |
GC collectors you will hear about: Serial (simple, single-thread), G1 (Garbage First) — the default since Java 9, good for big heaps, and ZGC — for huge heaps with very low pauses (Java 15+).
- Garbage Collection automatically frees memory of unused objects.
- An object becomes garbage when no reference reaches it (null, reassignment, method end, unreachable cycle).
- System.gc() is only a request — the JVM decides when to collect.
- finalize() is deprecated/removed; use try-with-resources or Cleaner instead.
- Objects live in the heap; modern GC uses Young and Old generations.
- G1 is the default GC; ZGC is for huge heaps with tiny pauses.