Sub topic 5
Reading garbage collection logs
This chapter assumes you know the heap and generations from the previous chapter, and that you can start a Java process from the command line (JDK 9 or later). A GC log records every collection, so you can see how often it runs, how long it pauses your threads, and whether memory is being reclaimed.
Turn on the log
Unified logging (JDK 9 and later) uses the -Xlog option:
java -Xlog:gc*:file=gc.log:time,uptime,level,tags -jar app.jargc*selects every log tag that starts withgc, so you get details beyond the one-line summary.file=gc.logwrites to a file instead of the console.time,uptime,level,tagsare decorators. They add a wall-clock timestamp, seconds since JVM start, the log level, and the tag set to each line.
Anatomy of a log line
A typical G1 pause line looks like this:
[2026-11-26T09:20:26.695+0000][12.345s][info][gc] GC(12) Pause Young (Normal) (G1 Evacuation Pause) 512M->128M(1024M) 18.456ms| Part | Meaning |
|---|---|
| `GC(12)` | The 13th collection since startup (numbering starts at 0). |
| `Pause Young (Normal)` | A young-generation collection that stopped application threads. |
| `512M->128M(1024M)` | Heap used before, heap used after, and total heap size. |
| `18.456ms` | How long the pause lasted. |
Reading it, the collection freed 384 MB and stopped your threads for about 18 ms. Pause time is what hurts request latency.
What to look for
- Pause duration. Compare the longest pauses with your latency goal. A 200 ms pause cannot fit inside a 100 ms p99 target.
- Frequency. Compute the interval between young collections from the uptime values. Collections every 100 ms point to a high allocation rate.
- Heap after collection. If the "after" figure rises steadily across collections, live data is growing. That suggests a leak or an undersized heap.
- Full collections. Lines containing
Pause Fullmean the collector could not keep up and paused everything. Treat these as a warning sign.
Watching a live process
When you cannot restart the application, jstat samples the heap from outside:
jstat -gcutil <pid> 1000It prints the percentage use of each heap region and cumulative GC counts and times every 1000 ms. If the old-generation column (O) keeps rising and the full-GC count (FGC) grows, the old generation is filling up and the collector is struggling.
Common mistakes
- Reading one line in isolation. One long pause may be an outlier. Look at the trend across hundreds of lines.
- Comparing logs from different collectors. G1, ZGC and Parallel word their lines differently. Check which collector produced the log before comparing numbers.
- Treating GC time as the whole story. A log with short pauses can still come from an application that allocates heavily and wastes CPU. The allocation chapter covers that case.
Exercise
A log shows Pause Young lines about every 200 ms. Each one reports the heap going from 900M to 150M, and each pause lasts 10 ms. Is this a leak? What does it suggest?
Show answer
It is not a leak. The heap returns to about 150M after every collection, so live data is stable. The frequent collections point to a high allocation rate, since about 750 MB is allocated every 200 ms. The 10 ms pauses are short, so this is only a problem if the allocation itself costs too much CPU. Allocation profiling is the next step.