JVM Internals and Garbage Collection
Heap structure, JIT compilation, class loading, garbage collectors (G1, ZGC, Shenandoah, Parallel, Serial), container sizing and diagnosing memory issues.
JVM architecture in brief
- Class loading: bootstrap, platform and application class loaders; CDS (class data sharing) and AOT cache (Java 24/25) speed startup.
- Execution: interpreter + tiered JIT (C1 for fast compilation, C2 for optimized code); GraalVM offers an alternative JIT and native images.
- Memory: heap (objects), metaspace (class metadata), thread stacks, code cache, direct/native memory.
flowchart TB SRC[.class files] --> CL[Class loaders] CL --> MS[Metaspace] CL --> EX[Interpreter] EX -->|hot methods| JIT[JIT: C1 → C2] JIT --> CC[Code cache] EX & CC --> HEAP[(Heap: young + old generations / regions)] HEAP --> GC[Garbage collector]
Garbage collectors
| Collector | Design | Best for | Pause behaviour |
|---|---|---|---|
| Serial | Single-threaded | Tiny heaps, single-CPU containers | Stop-the-world |
| Parallel | Multi-threaded throughput | Batch jobs where throughput matters more than pauses | Longer pauses |
| G1 (default since 9) | Region-based, generational, pause-time goal (-XX:MaxGCPauseMillis, default 200 ms) |
General purpose services | Typically tens to hundreds of ms |
| ZGC (generational since 21, only mode since 24) | Concurrent, colored pointers | Low latency, very large heaps (up to multi-TB) | Sub-millisecond pauses typical |
| Shenandoah (generational mode in 25) | Concurrent compaction | Low latency | Sub-10 ms pauses typical |
Container sizing
Since JDK 10 the JVM is container-aware. Prefer percentage flags:
java -XX:+UseZGC \
-XX:MaxRAMPercentage=75 \
-XX:+ExitOnOutOfMemoryError \
-Xlog:gc*:file=/var/log/gc.log:time,uptime:filecount=5,filesize=20m \
-jar app.jarLeave headroom for metaspace, thread stacks, direct buffers (Netty), and the code cache; set container memory limit = request to avoid noisy-neighbour OOM kills.
Diagnosing problems
| Symptom | Tools and approach |
|---|---|
| High GC time / pauses | GC logs (GCeasy, GCViewer), JFR, check allocation rate and live set |
| Memory leak (old gen grows) | Heap dump (jcmd <pid> GC.heap_dump), Eclipse MAT dominator tree |
| Native memory growth | Native Memory Tracking (-XX:NativeMemoryTracking=summary, jcmd VM.native_memory) |
| CPU hotspots | async-profiler flame graphs, JFR |
| Thread issues | Thread dumps (jcmd Thread.print), virtual thread dumps (jcmd Thread.dump_to_file -format=json) |
Interview questions and answers
G1 or ZGC?
G1 is a good default balancing throughput and pauses. Choose ZGC when tail latency matters (p99 SLOs), heaps are large, or GC pauses show up in latency, accepting slightly higher CPU use.
What is the difference between a memory leak and a high allocation rate?
A leak means the live set grows over time (objects remain reachable). A high allocation rate means many short-lived objects; GC frequency rises but the live set is stable. Fix leaks by finding retaining references; fix allocation churn with profiling and reuse.
Why did my container get OOM-killed when the heap was fine?
Non-heap memory (metaspace, thread stacks, direct buffers, native libraries) pushed total RSS over the container limit. Lower MaxRAMPercentage, cap direct memory, and use NMT to find the growth.