How to Read GC Logs and Find the Real Problem in Production
Turn on unified GC logging, read a G1 log line field by field and recognize the patterns for leaks, undersized heaps, long pauses and allocation spikes.

GC logs are the cheapest and most precise instrument you have, and most teams switch them on only after an outage. Turning them on permanently costs almost nothing and gives you a record of the heap's behavior. This post shows the setup, decodes a G1 line and lists the patterns that map to causes. The collectors are explained in Java garbage collection explained.
Turn it on
java -Xlog:gc*:file=gc.log:time,uptime,level,tags:filecount=5,filesize=20m -jar app.jarThis writes detailed GC events to gc.log, with timestamps, uptime and tags, rotating over five files of 20 MB. A shorter -Xlog:gc:file=gc.log writes one summary line per collection, which is enough to start. In a container, log to standard output with -Xlog:gc and let the log collector keep it.
Decode one line
A G1 young collection in the summary log:
[2.345s][info][gc] GC(12) Pause Young (Normal) (G1 Evacuation Pause) 512M->140M(1024M) 8.123ms| Part | Meaning |
|---|---|
[2.345s] | Time since JVM start |
GC(12) | The twelfth collection |
Pause Young (Normal) | A young-generation pause, normal type |
(G1 Evacuation Pause) | The cause: regular young collection |
512M->140M(1024M) | Heap used before, after, and the committed heap |
8.123ms | How long the application was paused |
Reading it: 372 MB was reclaimed in 8 ms, and 140 MB survived. Compare many lines rather than one.
Patterns and what they mean
A steadily rising floor. Look at the heap size after collections, the number after the arrow. If it rises across a long period and does not return to a baseline after concurrent cycles or Full GCs, objects are being retained. That is a leak; capture a heap dump. See GC overhead limit exceeded.
Frequent young collections. Lines every few hundred milliseconds with a large drop each time indicate a high allocation rate. Each pause is short, so this is often fine. If CPU goes to GC, find what allocates: a profiler's allocation view shows it.
Long young pauses. Pauses above your target mean a lot of live data to copy, or too few GC threads for the CPU quota. The line Pause Young (Normal) taking 150 ms where it took 10 before suggests the survival rate changed, for instance after a deployment that caches more.
Full GC. Any Pause Full in G1 is a failure of the concurrent machinery to keep up and stops the world for the whole heap. Causes include (G1 Evacuation Pause) failing with "to-space exhausted", allocation outrunning concurrent marking, or System.gc() calls ((System.gc()) in the cause). Treat each as a bug to fix.
Humongous allocations. (G1 Humongous Allocation) triggers a collection because an object larger than half a region was allocated. Large arrays and buffers cause it. Reduce their size or raise the region size, then re-measure.
Useful extra detail
-Xlog:gc* also prints the phases of each pause and the concurrent cycle (Concurrent Mark Cycle), and -Xlog:gc+heap+exit prints the heap at exit. For summaries across a long log, a tool such as GCViewer or a quick script computes the maximum pause and the percentage of time in GC. The number that matters is the share of wall-clock time spent paused:
total pause time / total uptimeSeveral percent or more means GC deserves attention. Under one percent rarely needs it.
Connect the log to the cause
- Pick the time of the incident from application logs.
- Find the GC lines around that time.
- Look for a long pause, a Full GC or a heap that stopped shrinking.
- Match the cause shown in parentheses.
- Change one thing, and compare the next log.
Tuning G1 covers what to change.
A program that gives the log something to say
To practice reading, generate a log from code you control. This program allocates short-lived garbage continuously and keeps a growing set of survivors, which is the shape of a leak:
import java.util.ArrayList;
import java.util.List;
public class GcDemo {
static final List<byte[]> retained = new ArrayList<>();
public static void main(String[] args) throws Exception {
for (int i = 0; ; i++) {
byte[] shortLived = new byte[64 * 1024]; // dies immediately
if (i % 200 == 0) {
retained.add(new byte[256 * 1024]); // never released
}
if (i % 5000 == 0) {
System.out.println("retained MB: " + (retained.size() >> 2));
Thread.sleep(50);
}
}
}
}Run it with a small heap so the effects show quickly:
java -Xmx512m -Xlog:gc:file=demo-gc.log GcDemo.javaEarly in the log you will see young collections that reclaim almost everything. As retained grows, the number after the arrow in each line climbs, the young pauses lengthen slightly and eventually concurrent cycles and then a Full GC appear before the process dies with OutOfMemoryError: Java heap space. That climbing floor is the pattern described above, and seeing it on a machine where you know the cause trains the eye for the moment it shows up in production.
What the worrying lines look like
The exact wording varies by version and flags, so match on the words rather than the layout. Illustrative shapes for the cases that matter:
[40.1s][info][gc] GC(212) Pause Young (Concurrent Start) (G1 Humongous Allocation) 700M->650M(1024M) 31.4ms
[41.8s][info][gc] GC(215) Pause Young (Normal) (G1 Evacuation Pause) 900M->880M(1024M) 96.7ms
[42.0s][info][gc] GC(216) Pause Full (G1 Compaction Pause) 1020M->870M(1024M) 1412.5msThe first shows a humongous allocation starting a concurrent cycle, which is a sign to look for large arrays. The second reclaims 20 MB out of 900, so almost everything is live and the collector is working hard for little. The third is the failure itself: a Full GC that stopped the application for 1.4 seconds and freed only about 150 MB. A sequence like the second and then the third, repeated, is a heap that is too small for its live set or a leak.
Compute the numbers instead of eyeballing
For a quick answer on a plain -Xlog:gc file, a few lines of awk give the maximum pause and the total paused time:
awk '/Pause/ { ms = $NF; sub("ms", "", ms); total += ms; if (ms > max) max = ms; n++ }
END { printf "pauses=%d total=%.0f ms max=%.1f ms\n", n, total, max }' gc.logDivide the total by the wall-clock time the log covers to get the pause share. For a larger window or a graph, load the file into GCViewer or GCeasy, but keep the habit of checking the three numbers: maximum pause, pause share, and the heap floor after collections.
Record more than the log when you can
A GC log answers "when and how long". JDK Flight Recorder answers "which allocation site", and it is cheap enough to leave on in production with the default profile:
java -XX:StartFlightRecording=filename=/tmp/app.jfr,maxage=1h,settings=default -jar app.jar
jfr print --events jdk.GarbageCollection,jdk.ObjectAllocationSample /tmp/app.jfr | head -60The allocation samples carry stack traces, so the line "allocation rate is high" becomes "this method in this class allocates 40 percent of the bytes". Keep the GC log for the timeline and the recording for the attribution, and capture both before restarting a misbehaving instance, because a restart erases the evidence.
Where logs are not enough
GC logs say when the collector ran. They do not say which object holds the memory or which code allocates; use a heap dump and a profiler (JDK Flight Recorder works in production) for that. The unified logging documentation and the GC tuning guide describe the options.


