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.

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: GC Tuning Guide: G1 tuning, java command: -Xlog, Troubleshooting Guide

Sources (0)
See inline citations in the answer above.