Sub topic 10
Reading GC logs
This chapter assumes you know the generational heap layout (eden, survivor spaces, old generation) and the main HotSpot collectors. It also assumes you can run java from a terminal. You will learn how to turn on unified logging, read the common line formats, and spot the patterns that show a memory problem.
Why read logs instead of guessing
A GC log is a time-stamped record of every collection. It shows how often the collector runs, how long it pauses your threads, and how much memory each run frees. Logging is cheap enough to leave on in production. The logs come from the JVM itself, so they are more reliable than a guess based on response times.
JDK 9 replaced the old -verbose:gc and -XX:+PrintGCDetails flags with unified logging, controlled by the single -Xlog option. The examples here use that syntax, which also works on JDK 11.
Turning logging on
The -Xlog option has this shape:
-Xlog:[what][:[output][:[decorators][:output-options]]]Each part is optional and parts are separated by colons. The simplest useful form is:
java -Xms512m -Xmx512m -XX:+UseG1GC -Xlog:gc*:file=gc.log LeakDemogc*selects thegctag and every tag combined with it, such asgc+heaporgc+phases. The*is a wildcard.file=gc.logsends output to a file. Without it, output goes to stdout.
Plain -Xlog:gc prints one summary line per collection. Plain -Xlog with no arguments logs all info-level messages to stdout.
Choosing detail and format
You can attach a level to the selector with =. The levels are off, error, warning, info, debug and trace. The default is info.
java -Xlog:gc*=debug:file=gc-debug.log:uptime,level,tags -cp . LeakDemoThe third field lists decorators, which are the prefixes on each line. The default for a file is uptime,level,tags.
| Decorator | Prefix it adds | Use |
|---|---|---|
| `uptime` | `[3.201s]` | Time since JVM start. Good for measuring intervals. |
| `time` | `[2026-11-26T06:43:58.249+0000]` | Wall-clock time. Needed to match a pause to an incident. |
| `level` | `[info]` | Severity of the message. |
| `tags` | `[gc,heap]` | Which log tags produced the line. |
| `pid` | `[12345]` | Process ID. Useful when several JVMs share a file. |
For production, add time so you can line up GC events with application logs.
Rotating files
A log that grows forever will fill a disk. Output options set rotation:
java -Xms512m -Xmx512m -XX:+UseG1GC \
-Xlog:gc*:file=gc.log:time,uptime,level,tags:filecount=5,filesize=10m \
LeakDemoThis keeps up to five files of about 10 MB each. When the size limit is reached, the JVM rotates to a new file and renames the old ones with numeric suffixes.
Anatomy of a single line
Here is a young collection from G1:
[0.512s][info][gc] GC(3) Pause Young (Normal) (G1 Evacuation Pause) 25M->8M(512M) 4.321msReading left to right:
- Decorators. The JVM had been running for 0.512 seconds.
GC(3). This is the fourth collection since start, because numbering begins at 0. The same number appears on related lines, so you can group them.Pause Young (Normal). This collection reclaimed the young generation. The wordPausemeans application threads were stopped.(G1 Evacuation Pause). The cause. G1 copies ("evacuates") live objects out of regions.25M->8M(512M). Heap in use was 25 MB before and 8 MB after. The committed heap is 512 MB.4.321ms. How long the pause lasted.
Two simple calculations come from this line. The memory freed is 25 − 8 = 17 MB. The allocation rate between two collections is the "before" value of the later one minus the "after" value of the earlier one, divided by the time between them.
Common pause types and causes
The text in parentheses tells you why a collection started. The exact wording varies by collector and JDK version, so treat these as examples.
| Log text | Meaning | What to check |
|---|---|---|
| `Pause Young (Normal)` | Routine young collection after eden filled. | Usually healthy. Look at frequency and duration. |
| `Pause Young (Concurrent Start)` | G1 young pause that also begins concurrent marking of the old generation. | Normal when old generation occupancy crosses its threshold. |
| `Pause Young (Mixed)` | G1 collects young regions plus some old regions. | Normal after marking finishes. |
| `Pause Full (Allocation Failure)` | A full, stop-the-world collection because an allocation could not be satisfied. | Almost always a problem. See below. |
| `Pause Full (System.gc())` | Code or a library called `System.gc()`. | Find the caller. Consider `-XX:+DisableExplicitGC` only after understanding why it is called. |
| `Pause Full (Metadata GC Threshold)` | Metaspace reached its limit. | Look for class loader leaks. |
Reading a healthy run versus a leaking one
Run LeakDemo from the earlier chapter with -Xmx256m and -Xlog:gc*:file=gc.log. It adds 1 MB arrays to a static list forever. A condensed excerpt of what you would see (values will differ on your machine):
[0.201s][info][gc] GC(0) Pause Young (Normal) (G1 Evacuation Pause) 26M->24M(256M) 3.110ms
[0.390s][info][gc] GC(1) Pause Young (Normal) (G1 Evacuation Pause) 70M->68M(256M) 5.870ms
[0.640s][info][gc] GC(2) Pause Young (Normal) (G1 Evacuation Pause) 140M->138M(256M) 9.400ms
[0.910s][info][gc] GC(3) Pause Full (Allocation Failure) 250M->248M(256M) 310.220ms
[1.230s][info][gc] GC(4) Pause Full (Allocation Failure) 255M->255M(256M) 295.804msThree signs show up here:
- The "after" number keeps climbing. 24M, 68M, 138M, 248M. A healthy program's post-collection usage rises and falls around a stable level. A steady upward trend means live data is growing.
- Collections free almost nothing.
250M->248Mreclaimed 2 MB at a cost of 310 ms. Everything is still reachable from a GC root, so the collector cannot help. - Full collections repeat. Back-to-back
Pause Fullentries with no recovery usually come right beforeOutOfMemoryError: Java heap space.
A healthy run looks different. The "after" values return to about the same level each time, and young pauses stay in the low milliseconds.
[10.100s][info][gc] GC(40) Pause Young (Normal) (G1 Evacuation Pause) 180M->42M(512M) 4.2ms
[10.480s][info][gc] GC(41) Pause Young (Normal) (G1 Evacuation Pause) 181M->43M(512M) 4.0ms
[10.860s][info][gc] GC(42) Pause Young (Normal) (G1 Evacuation Pause) 180M->42M(512M) 4.1msThe live set (the post-collection value) stays near 42 MB. Use it to size the heap, as the next chapter explains.
Concurrent phases and other collectors
G1 and ZGC do part of their work while the application runs. These phases are logged without the word Pause:
[5.020s][info][gc] GC(12) Concurrent Cycle
[5.071s][info][gc] GC(12) Concurrent Cycle 51.3msOnly the Pause lines count as stop-the-world time. A long concurrent phase is not itself a problem, but if it never completes before the heap fills, G1 falls back to a full collection. Look for Pause Full after a concurrent cycle that started late.
Other collectors print different shapes:
- Serial and Parallel log
Pause Young (Allocation Failure)andPause Full (Ergonomics)or(Allocation Failure), with the samebefore->after(capacity)pattern. - ZGC logs per-phase lines such as
Pause Mark Startthat last well under a millisecond, plus summary statistics. Its pauses do not grow with heap size, so watch allocation stalls instead.
Getting more detail
With gc* at info, you also get lines like these from the gc+heap and gc+phases tags:
java -Xlog:gc+heap=debug,gc+phases=debug:file=gc-detail.log:uptime,level,tags -Xmx512m LeakDemoUse gc+heap=debug to see per-region and per-generation sizes. Use gc+phases=debug to see how long each phase of a pause took, such as root scanning and object copying. This helps when a young pause is slower than expected.
Questions to ask of any log
- How often do pauses happen? Subtract consecutive uptimes.
- How long are they? Compare the maximum, not only the average, with your latency target.
- How much time goes to GC overall? Add up the pause durations over an interval and divide by the interval length.
- Does the live set stay flat or climb? Climbing points to a leak.
- Are there full collections? In G1, each one deserves an explanation.
- What is the cause text?
System.gc()andMetadata GC Thresholdpoint to different fixes thanAllocation Failure.
Common mistakes
- Reading only averages. One 2-second pause among thousands of 5 ms pauses will not show in the mean, but users will notice it.
- Comparing logs across collectors. A 200 ms pause means different things under Parallel and ZGC.
- Forgetting the decorators. Without
time, you cannot match a pause to an incident in an application log. - Trusting
usedto mean live. The "before" number includes garbage. Only the "after" number approximates live data. - Using the old flags.
-XX:+PrintGCDetailswas removed in JDK 9 and later versions treat it as an error or ignore it, depending on version. Use-Xlog.
Exercise
A G1 log shows these lines. What do they tell you, and what is your next step? [20.0s][info][gc] GC(8) Pause Young (Normal) (G1 Evacuation Pause) 400M->390M(512M) 25ms [20.9s][info][gc] GC(9) Pause Full (Allocation Failure) 505M->500M(512M) 640ms
Show answer
Collections free very little (10 MB, then 5 MB), and the post-collection size is near the 512 MB capacity, so almost all data is still reachable. A full collection with cause Allocation Failure confirms the heap is exhausted. The likely explanations are a leak or a heap that is too small for the live set. Next, capture a heap dump (for example with -XX:+HeapDumpOnOutOfMemoryError) or a class histogram with jcmd <pid> GC.class_histogram to see which objects dominate.
Exercise
Write the command line that runs LeakDemo with a 256 MB fixed heap, G1, and GC logging to gc.log with wall-clock time and uptime, rotating through three files of 5 MB each.
Show answer
java -Xms256m -Xmx256m -XX:+UseG1GC -Xlog:gc*:file=gc.log:time,uptime,level,tags:filecount=3,filesize=5m LeakDemo
Exercise
A log line reads [3.0s][info][gc] GC(5) Pause Young (Normal) (G1 Evacuation Pause) 60M->20M(256M) 6ms, and the next young collection is at 3.5s with 80M->25M(256M). Roughly how fast is the program allocating?
Show answer
After GC(5) the heap held 20 MB, and at the next collection it held 80 MB before collecting, so 60 MB were allocated in about 0.5 s, around 120 MB per second. The live set grew from 20 MB to 25 MB, which is worth watching over more collections.