What Is Garbage Collection in Java? — Reachability and Reference Strengths
Fast recall
Quick revision
Core answer
Whether you understand that GC is reachability-based, not reference-count-based, and know how to use weak/soft references to prevent memory leaks
Saying GC works on reference counting (it does not — that's Python's CPython implementation; Java uses tracing collectors that walk from GC roots)
Explain why circular references are not a memory leak in Java but can still leak through static collections, listener registrations, or ClassLoader retention
Interview-ready guide
Interview answer
Java's garbage collector automatically reclaims memory used by objects no longer reachable from the application. The key insight is that GC is reachability-based, not reference-count-based — it starts from a set of GC roots like thread stacks and static fields, and any object it cannot reach is garbage.
How GC decides what's garbage:
- Walks references from GC roots: thread stacks, static fields, JNI references, system class loader
- Anything unreachable is collected — including circular reference cycles, which reference counting cannot handle
- This is why Java does not need explicit
free()calls and why circular references do not leak
Reference strengths from strongest to weakest:
- Strong — the default
=assignment; pins objects in memory until they go out of scope - Soft — collected only under memory pressure; designed for caches but often worse than bounded caches like Caffeine
- Weak — collected at the next GC; powers
WeakHashMapandThreadLocalcleanup - Phantom — used for cleanup notification after finalization; backs the modern
CleanerAPI
Common misconception to address:
- GC does not prevent all memory leaks — it only collects unreachable objects
- The most common Java memory leaks are: static collections that grow forever, listener registrations that are never removed, and
ThreadLocalvalues in pooled threads
In an interview, I'd emphasize that understanding reachability is what lets you debug a production heap dump — knowing the GC algorithm is secondary.
What Is Garbage Collection in Java? — Reachability and Reference Strengths
Direct Answer
Garbage collection is the JVM's automatic process of reclaiming memory used by objects no longer reachable from any GC root (active threads, static fields, JNI references, the stack). Reachability — not reference count — decides eligibility, which is why circular references are collected correctly. Java exposes four reference strengths: strong (default, prevents collection), soft (collected only under memory pressure, useful for caches), weak (collected at the next GC, used in WeakHashMap and ThreadLocal), and phantom (used for cleanup notification after finalization).
Common Mistakes & Pitfalls
Saying GC works on reference counting (it does not — that's Python's CPython implementation; Java uses tracing collectors that walk from GC roots)
Check your understanding
Answer these without looking back at the article.
- What is the difference between WeakReference and SoftReference, and when would you use each?
- How would you debug a slow memory leak in a long-running Java service?
- Why was the `finalize()` method deprecated, and what should we use instead?
- What is a ClassLoader leak, and why does it survive a full GC?
Related interview prep categories
2Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.