跪拜 Guibai
← All articles
Backend · Java

Full GC Every 10 Minutes: A JDK 21 Diagnostic Walkthrough from jstat to JFR to a Code Fix

By 会编程的吕洞宾 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Full GC stalls are a top production killer for any Java service, and the debugging path shown here uses only tools already in the JDK. The concrete lesson generalizes beyond this demo: unbounded static caches and large arrays are the usual suspects, and fixing them beats tuning the collector.

Summary

A report service on JDK 21 shows a classic production failure: P99 jumps from 80ms to 8 seconds, CPU stays flat, and the GC log shows Full GC every 10 minutes with pauses over 3 seconds. The root cause is a static ConcurrentHashMap that caches 20MB report arrays and never evicts them. Under G1, each 20MB byte[] is a Humongous object that goes straight to the old generation, and the cache's strong references make those regions impossible to reclaim.

The diagnostic sequence is entirely JDK built-in tools. jstat -gcutil shows old-generation usage pinned above 90% with Full GC count climbing. JFR recording via jcmd points allocation hot spots directly at the new byte[20MB] call. jmap -histo:live confirms byte arrays dominate the heap, and a heap dump with MAT traces the reference chain to the static field. The fix adds a scheduled cleaner that clears the cache every 24 hours and swaps put-if-absent logic to ConcurrentHashMap.computeIfAbsent.

JVM tuning advice covers three parameters: InitialRAMPercentage and MaxRAMPercentage for container-aware heap sizing, MaxGCPauseMillis=100 for G1's pause target, and HeapDumpOnOutOfMemoryError plus unified GC logging for incident forensics. A comparison of Parallel, G1, and ZGC ends with the practical rule: no collector rescues leaky code; caches need a bound or an expiration.

Takeaways
— G1's old generation fills when objects are strongly referenced by static fields; a static Map that only puts never lets regions be reclaimed.
— Objects larger than half a G1 Region (20MB byte arrays in a 1MB Region setup) become Humongous and bypass the young generation entirely.
— jstat -gcutil output shows old-gen usage above 90% and Full GC count/time climbing; young GC count stays low, isolating the problem to old gen.
— JFR's allocation view lists the call stacks that allocate the most memory, directly exposing the new byte[20MB] site without restarting the app.
— jmap -histo:live lists object counts and sizes; byte arrays ([B) dominate when large buffers are the leak source.
— Dump the heap with jcmd GC.heap_dump only after histogram and JFR point to the culprit; MAT's Path to GC Roots confirms the static field reference.
— The code fix uses ConcurrentHashMap.computeIfAbsent plus a scheduled executor that clears the cache every 24 hours.
— JVM parameters: InitialRAMPercentage and MaxRAMPercentage (not both with -Xmx), MaxGCPauseMillis=100, HeapDumpOnOutOfMemoryError, and -Xlog:gc*.
— Avoid common pitfalls: raising -Xmx without fixing the leak makes Full GC pauses longer; setting both -Xmx and MaxRAMPercentage gives unpredictable behavior; dumping a multi-GB heap before checking the histogram wastes time.
— Choose collectors by use case: Parallel for throughput, G1 for most online services, ZGC only for heaps over 16GB with sub-millisecond pause needs.
— Always enable HeapDumpOnOutOfMemoryError, GC logging, and always-on JFR (under 1% CPU overhead) in production.
— Monitor jvm_gc_pause_seconds and jvm_gc_overhead; alert when GC overhead exceeds 10%.
Conclusions

The diagnostic order matters more than any single tool: jstat narrows the generation, JFR finds the allocation site, jmap confirms the object type, and only then does the code fix become obvious.

A cache without an eviction policy is a time bomb regardless of the garbage collector; tuning collector parameters only postpones the inevitable and often makes pauses worse.

JFR being always-on in production is the single highest-leverage habit described; the 1% CPU cost is trivial compared to a 30-minute heap dump transfer.

The article's advice to avoid setting both -Xmx and MaxRAMPercentage is a reminder that container-aware JVM defaults have changed; blindly carrying old flags into Kubernetes breaks memory accounting.

Concepts & terms
Full GC
A stop-the-world garbage collection that reclaims the entire JVM heap, including the old generation. Under G1 it runs single-threaded and can pause all application threads for seconds, causing interface timeouts.
G1 (Garbage First) collector
The default garbage collector in JDK 21. It divides the heap into equal-sized regions and prioritizes reclaiming those with the most garbage, aiming for predictable pause times via an ergonomic pause target.
Humongous object
An object larger than half the size of a G1 region. G1 allocates it directly in a contiguous region that acts like the old generation, bypassing young-generation promotion and often causing premature old-gen pressure.
JFR (Java Flight Recorder)
A built-in sampling profiler in the JDK. It records events like GC pauses and object allocations with low overhead, and can be started and stopped remotely via jcmd without restarting the application.
IHOP (Initiating Heap Occupancy Percent)
The threshold (default 45%) of old-generation occupancy that triggers G1's concurrent marking cycle. If Mixed GC can't reclaim fast enough after marking, old generation fills and Full GC follows.
Mixed GC
A G1 collection that reclaims young generation plus a subset of old regions, sized to stay within the MaxGCPauseMillis target. It is best-effort and can be overwhelmed by continuous old-gen growth.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗