How does garbage collection decide what to free — and what are GC roots?
Reviewed by Gurusankar M.
Short Answer
GC frees objects that are unreachable from GC roots (stack locals, statics, JNI refs, active threads). Mark: traverse from roots marking live objects; Sweep: reclaim unmarked; Compact: defragment. Java GC is reachability-based, not reference-counting, so it handles cycles.
On this page
Concept Made Simple
Coffee Chat Question
“How does garbage collection decide what to free — and what are GC roots?”
Remember It Faster
Mind Map Answer
- GC roots
- stack locals, statics, threads, JNI
- Mark
- walk graph from roots → live set
- Sweep
- reclaim everything unmarked
- Compact
- defragment surviving objects
Think Beyond the Expected
What If?
Two objects reference each other but nothing else points to them — are they collected?
Yes. Java uses reachability from GC roots, not reference counting, so an isolated cycle is unreachable and gets collected. This is why Java doesn't leak on reference cycles (unlike naive ref-counting).
Real World
'Path to GC roots' in a heap analyzer (MAT) is exactly this concept — to fix a leak you find what root still references the object that should be dead.
Interviewer's Expectation
Keywords they're listening for:
- reachability from roots
- what GC roots are
- mark-sweep-compact
- handles cycles
- not reference counting
Common Mistakes
- Thinking Java uses reference counting
- Assuming reference cycles leak
- Confusing 'unreachable' with 'null'
Best Practices
- Use MAT 'path to GC roots' for leak hunting
- Null out long-lived references when done (rarely needed)
- Understand roots to reason about retention
Follow-up Questions
- Why doesn't Java use reference counting?
- What does 'path to GC roots' tell you in a heap dump?
- What is a safepoint and why does GC need one?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Garbage Collection (JVM) Interview question: "How does garbage collection decide what to free — and what are GC roots?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair