Sub topic 10
Reading GC Logs
This chapter assumes you understand the heap, young and old generations, and the basic differences between Java garbage collectors. You should also be able to run a Java program from a command line.
GC logs show when collection occurs, why it started, how much memory was reclaimed, and how long the JVM paused application threads. They help you distinguish normal collection from allocation pressure, promotion problems, and a failing heap.
Enabling unified GC logging
Java 11 uses unified logging. The -Xlog option selects log tags and levels, then sends the messages to the console or a file.
java -Xms512m -Xmx512m -XX:+UseG1GC \
-Xlog:gc*,safepoint=info:file=gc.log:time,uptime,level,tags:filecount=5,filesize=10M \
-jar application.jarThis command:
- Sets the initial and maximum Java heap to 512 MB.
- Selects the G1 collector.
- Enables all
gctags andsafepointmessages at theinfolevel. - Writes the log to
gc.log. - Adds timestamps, uptime, levels, and tags.
- Rotates the file after it reaches 10 MB, keeping five files.
Use -Xlog:help to list available tags, levels, decorators, and output formats. Common levels are error, warning, info, debug, and trace. Start with info. More detailed levels can produce large files.
Understanding a GC event
A simplified G1 log might contain these lines:
[2026-11-26T07:02:11.431+0000][0.123s][info][gc,start] GC(0) Pause Young (Normal) (G1 Evacuation Pause)
[2026-11-26T07:02:11.436+0000][0.128s][info][gc,heap] GC(0) Eden regions: 12->0(10)
[2026-11-26T07:02:11.437+0000][0.129s][info][gc] GC(0) Pause Young (Normal) 64M->18M(512M) 5.8msThe parts have different meanings:
0.123sis the JVM uptime when the event began.gc,startidentifies the event as a garbage-collection start message.GC(0)is the collection identifier. Related lines share this identifier.Pause Youngmeans application threads were paused for a young-generation collection.64M->18M(512M)means heap usage changed from 64 MB to 18 MB. The number in parentheses is the current heap capacity.5.8msis the pause duration.
The before-and-after values describe heap occupancy, not the amount allocated by the application during that interval. A short pause that reclaims much memory can be healthy. A long pause that reclaims little memory deserves investigation.
Reading patterns instead of single events
One collection rarely explains a production problem. Look for patterns over time.
| Pattern | Possible meaning | What to check |
|---|---|---|
| Frequent young pauses with low occupancy afterward | High allocation rate | Allocation volume and request traffic |
| Old-generation occupancy keeps rising | Objects live longer than expected | Caches, queues, retained requests |
| Full collections occur repeatedly | Heap pressure or promotion failure | Heap size, live set, and leak suspects |
| Pauses exceed the latency target | Collector or heap configuration mismatch | Pause goals, object layout, and collector choice |
A G1 log may also show concurrent marking, remembered-set work, evacuation failures, or humongous allocations. Humongous objects occupy regions directly and can increase pressure when large arrays or buffers are created repeatedly.
For a quick summary, search a log for Pause, Full, Humongous, and to-space exhausted. Compare event frequency, pause duration, and post-collection occupancy. A rising post-GC baseline is often more important than a single high pre-GC value.
From logs to action
If pauses are frequent but short, increasing the heap may reduce frequency, but it also increases the amount of memory that can be scanned. If pauses are long, inspect the collector, live-set size, and object layout before simply adding memory. A heap cannot solve a leak when the retained object graph continues to grow.
Use Java Flight Recorder to correlate allocations and pauses with application code. Use a heap dump when the post-GC live set grows unexpectedly. For repeatable comparisons, change one setting at a time and measure the same workload.
Exercise
A log shows 120M->118M(512M) after a long young-collection pause. What does this suggest?
Show answer
The collection reclaimed very little memory. The application may have a high live set, objects may be promoted quickly, or the event may involve expensive evacuation work. Inspect related GC lines and allocation behavior instead of assuming that increasing the heap will fix it.