Java and Spring Level 3 This level · 11 of 56
Hard java java-21

How do you investigate frequent GC pauses?

The 30 second answer

I turn on GC logging and look at how often pauses happen, how long they are, whether they're young or full collections, and how much heap is still used after each one. Frequent young pauses usually mean a high allocation rate. Old-generation usage that keeps climbing after every collection suggests a leak. Full collections in G1 usually mean the heap is too small. Then I reduce allocation, resize, or consider ZGC.

Could you say this in an interview?

The answer I'd give out loud

I start with evidence, not guesses. Before touching any flag I turn on unified GC logging with -Xlog:gc*, so every pause shows its type, its duration and the heap before and after it. A typical G1 line looks like Pause Young (Normal) (G1 Evacuation Pause) 23M->3M(64M) 5.664ms. Then I read the pattern. Many short young pauses mean allocation pressure: the service creates garbage faster than the young generation can absorb it. Long pauses, old-generation or full collections, or heap after GC that climbs and never comes back down point at the old generation filling up. That is either a heap that is too small for the live data, or a leak.

For allocation pressure I record a JDK Flight Recorder profile and open it in JDK Mission Control. Its allocation view shows the allocation rate and the stack traces that allocate the most, so I can fix the code: reuse buffers, stop building large intermediate collections, and stream results instead of loading them all at once. If the live data is legitimately bigger than the heap, I size the heap from that measurement instead.

For a suspected leak I take a heap dump and open it in Eclipse Memory Analyzer (MAT). The dominator tree shows which objects keep the most memory alive, and the path to GC roots shows who holds them. Two dumps taken some time apart show what keeps growing.

On an Elasticsearch node, Elastic's troubleshooting guide lists the usual causes of high JVM memory pressure: a heap that is too small for heavy indexing and search load, large bulk or multi-search requests, expensive searches and aggregations with many buckets, field data on high-cardinality fields, and too many shards or segments. Its target is a young GC under 50 ms about once every 10 seconds, and an old GC under 1 second no more than once every 10 minutes. In our own Java service that talks to Elasticsearch, retained references are the usual suspect: unbounded caches, listeners that are registered and never removed, and bulk request batches that are kept after they are sent.

So the fix follows the diagnosis. Allocation pressure is fixed by allocating less in the hot code, or by sizing the heap from measured live data. A leak is fixed in code, after the heap dump shows the retaining path. Raising -Xmx on a leak only delays the outage.

Useful GC debugging flags

# Every GC event, written to rotated files
-Xlog:gc*:file=gc.log:time,uptime,level,tags:filecount=5,filesize=20m

# Heap layout before and after each collection
-Xlog:gc+heap=debug

# Tenuring: desired survivor size and the object age distribution
-Xlog:gc+age=trace

# Write a heap dump automatically when an OutOfMemoryError is thrown
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/var/log/app/heapdumps

# The three logging options combined into one file
-Xlog:gc*,gc+heap=debug,gc+age=trace:file=gc.log:time,uptime,level,tags

Without a file output, -Xlog writes to standard output. The gc+age=trace output prints an age table with the bytes at each object age, which shows whether objects die young or are promoted to the old generation. Make sure the heap dump path has enough disk space for a file about as large as the heap.

The investigation step by step

1. Get data:

-Xlog:gc*:file=gc.log:time,uptime,level,tags:filecount=5,filesize=20m

2. Read the pattern:

SymptomLikely causeAction
Very frequent young pauseshigh allocation rate or small young genreduce allocation (JFR allocation profiling), give more heap
Long young pausesmany surviving objects to copyfewer long-lived temporary objects, check promotion
Old gen after GC keeps risingmemory leakheap dump and class histogram
G1 full GCs or evacuation failuresheap too small, or marking starts too latemore heap, lower -XX:InitiatingHeapOccupancyPercent
Humongous allocations in G1objects of at least half a regionlarger -XX:G1HeapRegionSize, or avoid huge arrays

3. Tune carefully: set -Xms equal to -Xmx for steady services, adjust -XX:MaxGCPauseMillis, and avoid fixing the young-generation size with G1. If pause time is the main problem on a large heap, test ZGC.

[Opinion] Most GC problems are solved by allocating less, not by tuning flags.

Sources (6)
  1. GC Tuning Guide: G1 tuningdocs.oracle.com
  2. java command: -Xlogdocs.oracle.com
  3. Troubleshooting Guidedocs.oracle.com
  4. Troubleshooting Guide (Java 21): Diagnostic Tools, Flight Recorder and Mission Controldocs.oracle.com
  5. Eclipse Memory Analyzer (MAT)eclipse.dev
  6. Elasticsearch: High JVM memory pressureelastic.co
Previous
Next