GC Tuning and GC Log Analysis
Fast recall
Quick revision
Core answer
Practical ability to diagnose and fix GC problems, not just know GC theory
Tuning GC flags blindly without first collecting and analyzing GC logs
Explain the difference between minor GC pauses (normal, short) and full GC pauses (problematic) and what triggers a full GC
Interview-ready guide
Interview answer
GC tuning in Java follows a strict discipline: measure first, diagnose second, change one flag at a time. Without baseline telemetry from GC logs, you're guessing — and changing heap sizes or GC algorithms without data typically makes things worse, not better.
Step 1 — Enable GC logging:
-Xlog:gc*on Java 9+ — negligible overhead; provides all the telemetry needed for tuning decisions
Step 2 — Distinguish minor GC from full GC:
- Minor GC: reclaims young generation; typically 10-50ms; normal and expected
- Full GC: stops all application threads; can take seconds on large heaps; causes p99 latency spikes visible to users
Diagnosing frequent full GC:
- Post-GC live set stable but heap fills quickly → heap undersized; increase
-Xmx - Live set grows steadily over hours → memory leak; capture heap dump for analysis
- Sporadic full GC with no heap pressure → explicit
System.gc()from monitoring agent or RMI; add-XX:+DisableExplicitGC
I recommend always pairing GC logging with -XX:+HeapDumpOnOutOfMemoryError — when OOM strikes, you need the snapshot immediately rather than scrambling to reproduce after a restart.
GC Tuning and GC Log Analysis
Direct Answer
Enable GC logging with -Xlog:gc* and analyze with GCEasy or JVM Mission Control. Key metrics: pause frequency, pause duration, allocation rate, and promotion failure. Tune heap sizing, generation ratios, and target pause time based on findings.
Deep Dive & Architecture
ZGC and Shenandoah achieve sub-millisecond pauses by performing all GC work — including compaction — concurrently with application threads.
ZGC (Oracle, production-ready in Java 15) uses colored pointers and load barriers to relocate objects without stopping the world.
Shenandoah (Red Hat, production-ready in Java 15) uses Brooks pointers (forwarding pointers in object headers) for the same goal. Both target heaps from a few hundred MB to multiple TB with pauses under 10ms — and typically under 1ms. The trade-off is 5-15% additional CPU overhead and modest extra memory for the relocation metadata. Use them when p99 latency requirements are stricter than what G1's -XX:MaxGCPauseMillis=200 can deliver.
Saying GC works on reference counting (it does not — that's Python's CPython implementation; Java uses tracing collectors that walk from GC roots)
Common Mistakes & Pitfalls
Tuning GC flags blindly without first collecting and analyzing GC logs
Check your understanding
Answer these without looking back at the article.
- What is garbage collection in Java? Explain reachability and the different reference strengths.
- Explain generational garbage collection in Java. What are Eden, Survivor spaces, and the Tenured generation, and how does promotion work?
- Compare Java's garbage collectors: G1GC, ZGC, Shenandoah, and ParallelGC.
Related interview prep categories
2Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.