Mastering JVM Garbage Collection: Interview Questions and Deep Dive
This article provides a comprehensive overview of JVM memory layout, explains major garbage‑collection algorithms and collectors, details how different memory regions cooperate, and offers practical tuning steps and tool usage for diagnosing GC issues in Java applications.
1. JVM Heap Partition from a GC Perspective
Java heap is divided into the old generation and the young generation; the young generation further consists of Eden, Survivor0 (S0) and Survivor1 (S1) spaces.
Why split the heap?
Over 90% of Java objects are short‑lived, so placing newly created objects in Eden and promoting long‑lived objects to the old generation improves performance.
Why further split the young generation?
Eden fills quickly; surviving objects are moved to Survivor spaces, providing a buffer that reduces the frequency of Full GC.
Do Survivor spaces undergo GC?
Yes, they are reclaimed during Young GC as part of the Eden collection (default ratio 8:1:1).
Why not just Eden and one Survivor?
Two Survivor spaces enable the copying algorithm, preventing fragmentation by always keeping one Survivor empty for the next copy cycle.
2. JVM Garbage‑Collection Algorithms
Seven common algorithms are described:
1. Reachability Analysis (Mark Phase)
Starts from GC Roots (stack, method area, string pool, etc.) and marks reachable objects.
2. Mark‑Sweep (Young Generation)
Stops the world, marks reachable objects, then sweeps unmarked ones; drawbacks include pause time, low efficiency, and memory fragmentation.
3. Copying Algorithm (Young Generation)
Copies live objects to a new space, leaving the old space free; advantages are contiguous memory, but it requires extra space.
4. Mark‑Compact (Old Generation)
Marks reachable objects, then compacts them to one end of the heap, eliminating fragmentation.
5. Generational Collection
Uses different algorithms for young (copying) and old (mark‑sweep or mark‑compact) generations.
6. Incremental Collection
Interleaves GC work with application threads to reduce pause times, at the cost of higher overall overhead.
7. Region‑Based (G1 Collector)
Divides the heap into equal regions, allowing independent collection of selected regions.
3. Common Garbage Collectors and Their Pros/Cons
Young‑Generation Collectors
Serial : Single‑threaded copying collector; pauses all application threads.
ParNew : Multithreaded version of Serial.
Parallel Scavenge : Multithreaded, aims for high throughput rather than minimal pause.
Old‑Generation Collectors
Serial Old : Single‑threaded mark‑compact collector.
Parallel Old : Multithreaded mark‑compact collector.
CMS (Concurrent Mark‑Sweep) : Minimizes pause time by performing most work concurrently; consists of initial mark, concurrent mark, remark, and concurrent sweep phases.
Heap‑Wide Collectors
G1 : Default in JDK 9, partitions the heap into regions and performs incremental collection, offering predictable pause times.
4. Coordination of JVM Memory Areas
Runtime data areas include Method Area (non‑heap), Java Stack, Native Stack, Heap (young and old generations), and Program Counter. Class loaders, execution engine, and native libraries are non‑runtime areas.
Stacks reference objects in the heap; the Method Area holds class metadata; after JDK 8, class metadata resides in Metaspace (off‑heap).
5. G1 Collector Overview
6. Why CMS Pauses Twice?
CMS performs an initial mark (short STW), then concurrent marking, a remark phase (second STW) to capture objects modified during concurrent marking, and finally concurrent sweep.
7. System.gc() vs Runtime.getRuntime().gc()
Both request a Full GC, but the JVM may ignore the request; explicit GC can be disabled with -XX:+DisableExplicitGC .
8. JVM Tuning Workflow
1) Identify whether performance issues stem from code or JVM parameters. 2) Monitor using -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -Xloggc:gc.log and capture heap dumps with -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=... . 3) Analyze dumps with tools like jvisualvm or MAT. 4) Diagnose Full GC and Young GC frequencies, adjust -Xms/-Xmx , -Xmn , and enable/disable explicit GC as needed.
9. When Does GC Trigger?
Minor GC occurs when Eden is full; Full GC triggers when promotion fails, old‑generation usage exceeds thresholds, Metaspace is exhausted, or System.gc() is invoked.
10. Using Diagnostic Tools (jstat, jmap, jstack, MAT)
Find the Java process ID, then:
Use jstat -gc pid to view memory usage and GC statistics.
Use jstack pid to inspect thread stacks for deadlocks or high CPU.
Generate heap dumps with jmap -dump:format=b,file=demo.hprof pid and analyze with jvisualvm or MAT.
Additional commands: jmap -histo pid , jmap -heap pid .
Sanyou's Java Diary
Passionate about technology, though not great at solving problems; eager to share, never tire of learning!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.