Skip to lesson

Reading a GC log

Each pause in a GC log tells you what kind of collection ran, why, how much memory was in use before and after, and how long the program stopped. Read across many lines and you can tell a healthy program from one that is leaking memory or short of heap.

Reading one line: type, cause, heap and time

A GC log line records one collection: when it happened, its type and cause, the heap used before and after with the total size, and how long the program was paused.

A GC log line for one pause has the same parts every time: when it happened, the GC number, the pause type, the cause in brackets, the heap before and after the collection with the heap size, and how long the program stopped.

One G1 log line taken apart: [1.234s] time since start; [info][gc] level and tag; GC(3) the fourth collection; Pause Young (Normal) the type; (G1 Evacuation Pause) the cause; 23M->1M(118M) heap before, after and total; 4.2ms the pause length.

Example 1 · Taking one G1 line apart

GcLineParser.javaJava
import java.util.regex.Matcher;import java.util.regex.Pattern;
public class GcLineParser {    // uptime, GC number, pause type, cause, heap before, after, heap size, pause time    static final Pattern LINE = Pattern.compile(            "\\[([^\\]]+)\\].*GC\\((\\d+)\\) (Pause \\w+(?: \\(\\w+\\))?) \\((.*?)\\) (\\d+)M->(\\d+)M\\((\\d+)M\\) ([\\d.]+)ms");
    public static void main(String[] args) {        String line = "[0.267s][info][gc] GC(10) Pause Young (Mixed) (G1 Evacuation Pause) 43M->13M(64M) 1.211ms";        Matcher m = LINE.matcher(line);        if (m.find()) {            System.out.println("When:        " + m.group(1) + " after JVM start");            System.out.println("GC number:   " + m.group(2));            System.out.println("Pause type:  " + m.group(3));            System.out.println("Cause:       " + m.group(4));            System.out.println("Heap before: " + m.group(5) + " MB");            System.out.println("Heap after:  " + m.group(6) + " MB  <- watch this number over time");            System.out.println("Heap size:   " + m.group(7) + " MB");            System.out.println("Pause:       " + m.group(8) + " ms");        }    }}
Output
When:        0.267s after JVM start
GC number:   10
Pause type:  Pause Young (Mixed)
Cause:       G1 Evacuation Pause
Heap before: 43 MB
Heap after:  13 MB  <- watch this number over time
Heap size:   64 MB
Pause:       1.211 ms

What this code does

  • The regular expression has one group for each part of the line, in the order they appear. .*? (a lazy match) stops the cause at the first ) that is followed by the heap numbers.
  • The same parts appear in every G1 summary line, so a small parser like this can turn a long log into numbers you can compare or chart.

Take [0.267s][info][gc] GC(10) Pause Young (Mixed) (G1 Evacuation Pause) 43M->13M(64M) 1.211ms. It is the 11th collection (counting from 0), 0.267 s after start. It was a mixed pause, caused because G1 needed free regions ("Evacuation Pause"). The heap held 43 MB before and 13 MB after, out of a 64 MB heap, and the program was stopped for 1.211 ms. The most useful number is the heap after GC. It is close to the data your program still uses: exact after a full GC, and a little high after a young GC, because old garbage not yet collected is counted too.

Pause Young (Normal), Pause Young (Mixed) and Pause Full

Pause Young (Normal) is G1's everyday pause: eden is full, so its live objects are copied out and eden is emptied. Most lines in a healthy log are this type.

Example 2 · Normal, mixed and full pauses in real logs

Terminal
java -Xmx64m -Xlog:gc ResultsCacheDemo       # from the G1 lessonjava -Xlog:gc SystemGcDemo                   # a program that calls System.gc()java -Xmx48m -Xlog:gc SessionLeakDemo         # Example 4 below
Sample output
[0.162s][info][gc] GC(0) Pause Young (Normal) (G1 Evacuation Pause) 23M->10M(64M) 8.953ms
...
[0.224s][info][gc] GC(4) Pause Young (Concurrent Start) (G1 Evacuation Pause) 45M->40M(64M) 4.321ms
...
[0.252s][info][gc] GC(9) Pause Young (Prepare Mixed) (G1 Evacuation Pause) 29M->18M(64M) 0.972ms
[0.267s][info][gc] GC(10) Pause Young (Mixed) (G1 Evacuation Pause) 43M->13M(64M) 1.211ms
...

[0.020s][info][gc] Using G1
[0.180s][info][gc] GC(0) Pause Young (Concurrent Start) (G1 Humongous Allocation) 56M->54M(118M) 2.595ms
[0.180s][info][gc] GC(1) Concurrent Mark Cycle
[0.186s][info][gc] GC(2) Pause Full (System.gc()) 61M->0M(8M) 3.922ms
...

...
[0.628s][info][gc] GC(84) Pause Young (Normal) (G1 Evacuation Pause) (Evacuation Failure) 46M->46M(48M) 0.488ms
[0.631s][info][gc] GC(85) Pause Full (G1 Compaction Pause) 46M->45M(48M) 2.727ms
...

What this code does

  • The first run shows normal, concurrent start, prepare mixed and mixed pauses. The second program fills 30 MB with 1 MB arrays (humongous objects), drops them and calls System.gc(), which G1 turns into a Full pause.
  • The third run is the leak shown in Example 6. When G1 found no free region to copy into ("Evacuation Failure"), it fell back to a Full pause that freed just 1 MB. Lines marked ... were trimmed.

Pause Young (Mixed) also cleans some old regions after a marking cycle; "Concurrent Start" and "Prepare Mixed" are young pauses that lead up to it. Pause Full means the whole heap was collected in one stop-the-world pause. The cause tells you why: (System.gc()) means code asked for it, and (G1 Compaction Pause) means G1 ran out of free regions and had no other way left. Serial and Parallel use simpler words. Their young pauses say (Allocation Failure), which only means "eden was full", not that something failed.

  • G1 Evacuation Pause: the normal reason; G1 needs space and copies live objects out.
  • G1 Humongous Allocation: a large object needed regions, so a collection started early.
  • Evacuation Failure: there was no free region to copy into; the heap is nearly full.
  • System.gc() or G1 Compaction Pause on a Full pause: find out why it happened.

Predict the output

Read the pause lines

Read the log lines and pick the right answer.

Round 1 of 3

How much memory did the first pause free?

[0.162s][info][gc] GC(0) Pause Young (Normal) (G1 Evacuation Pause) 23M->10M(64M) 8.953ms
[0.201s][info][gc] GC(1) Pause Young (Normal) (G1 Evacuation Pause) 33M->10M(64M) 2.100ms

Signs of trouble

A warning sign in a GC log is a pattern across many lines, not one bad line. Look at the last few minutes of the log and ask four questions.

Example 3 · Checking the warning signs with a program

LogHealthCheck.javaJava
import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;
public class LogHealthCheck {    static final Pattern SUMMARY = Pattern.compile("(\\d+)M->(\\d+)M\\((\\d+)M\\) ([\\d.]+)ms");
    public static void main(String[] args) {        String[] log = {   // real -Xlog:gc lines: SessionLeakDemo with -Xmx48m (trimmed)            "GC(0) Pause Young (Normal) (G1 Evacuation Pause) 23M->2M(48M) 30.654ms",            "GC(3) Pause Young (Normal) (G1 Evacuation Pause) 33M->11M(48M) 2.522ms",            "GC(6) Pause Young (Normal) (G1 Evacuation Pause) 40M->19M(48M) 2.398ms",            "GC(11) Pause Young (Normal) (G1 Evacuation Pause) 42M->28M(48M) 1.666ms",            "GC(20) Pause Young (Normal) (G1 Evacuation Pause) 42M->35M(48M) 1.519ms",            "GC(82) Pause Young (Normal) (G1 Evacuation Pause) 46M->45M(48M) 0.579ms",            "GC(85) Pause Full (G1 Compaction Pause) 46M->45M(48M) 2.727ms",            "GC(87) Pause Full (G1 Compaction Pause) 46M->45M(48M) 4.092ms"        };
        List<Integer> afterGc = new ArrayList<>();        int fullGcs = 0;        double longest = 0;        int lastSize = 0;        for (String line : log) {            Matcher m = SUMMARY.matcher(line);            if (!m.find()) {                continue;            }            afterGc.add(Integer.parseInt(m.group(2)));            lastSize = Integer.parseInt(m.group(3));            longest = Math.max(longest, Double.parseDouble(m.group(4)));            if (line.contains("Pause Full")) {                fullGcs++;            }        }
        int last = afterGc.get(afterGc.size() - 1);        System.out.println("Heap after GC: " + afterGc);        System.out.println("Full GCs: " + fullGcs);        System.out.println("Longest pause: " + longest + " ms");        System.out.println("Live data fills " + (last * 100 / lastSize) + "% of the heap after GC");        if (afterGc.get(0) < last && fullGcs > 0) {            System.out.println("Warning: heap after GC keeps rising and full GCs have started");        }    }}
Output
Heap after GC: [2, 11, 19, 28, 35, 45, 45, 45]
Full GCs: 2
Longest pause: 30.654 ms
Live data fills 93% of the heap after GC
Warning: heap after GC keeps rising and full GCs have started

What this code does

  • The program reads real summary lines from the leaking run and collects the heap after each GC, the number of Full pauses and the longest pause.
  • The heap after GC climbs from 2 MB to 45 MB, live data fills 93% of a 48 MB heap and two Full pauses have started. The 30 ms longest pause is the warm-up pause GC(0), so pause length is not the problem here.
  • Does the heap after GC keep rising from one collection to the next, even after mixed or full GCs? Live data is growing: often a leak.
  • Are there Full pauses, or many of them close together? The collector cannot keep up.
  • Are pauses longer than your users can wait, or than MaxGCPauseMillis?
  • Are collections back-to-back, a few milliseconds apart? The program spends a large share of its time paused.
A single slow first pause right after start-up is normal: the JVM is still warming up. Judge the log by its steady part.

A healthy log versus a leaking one

A healthy log shows a sawtooth: the heap fills up between pauses and drops back to about the same low level after each one. Pauses are evenly spaced and short, and there are no Full pauses.

Example 4 · An online store that forgets to drop sessions

SessionLeakDemo.javaJava
import java.util.HashMap;import java.util.Map;
class Session {    final String user;    final byte[] cart = new byte[100 * 1024];   // 100 KB of cart and wish-list data
    Session(String user) {        this.user = user;    }}
public class SessionLeakDemo {    static Map<Integer, Session> sessions = new HashMap<>();
    public static void main(String[] args) {        boolean fixed = args.length > 0 && args[0].equals("fixed");
        for (int order = 1; order <= 450; order++) {            sessions.put(order, new Session("user" + order));            browseCatalogue();                       // short-lived garbage for each order            if (fixed) {                sessions.remove(order);         // order delivered: drop the session            }        }        System.out.println((fixed ? "Fixed" : "Leaky") + " run, sessions still held: " + sessions.size());    }
    static int browseCatalogue() {        int chars = 0;        for (int i = 0; i < 20_000; i++) {            String line = "Dish " + (i % 50) + " costs Rs " + (i % 50) * 10;            chars += line.length();        }        return chars;    }}
Output
Leaky run, sessions still held: 450

What this code does

  • Each order adds a 100 KB Session to a static map. Without fixed, sessions are never removed, so all 450 (about 45 MB) stay reachable.
  • browseCatalogue creates short-lived strings, so young collections keep happening in both runs. With fixed, the session is removed once the order is delivered.

Example 5 · The healthy log: a steady sawtooth

Terminal
java -Xmx48m -Xlog:gc SessionLeakDemo fixed
Sample output
[0.033s][info][gc] Using G1
[0.256s][info][gc] GC(0) Pause Young (Normal) (G1 Evacuation Pause) 23M->0M(48M) 2.954ms
[0.284s][info][gc] GC(1) Pause Young (Normal) (G1 Evacuation Pause) 27M->0M(48M) 1.745ms
[0.306s][info][gc] GC(2) Pause Young (Normal) (G1 Evacuation Pause) 27M->0M(48M) 1.527ms
...
[0.472s][info][gc] GC(11) Pause Young (Normal) (G1 Evacuation Pause) 27M->0M(48M) 1.643ms
[0.490s][info][gc] GC(12) Pause Young (Normal) (G1 Evacuation Pause) 27M->0M(48M) 1.721ms
[0.510s][info][gc] GC(13) Pause Young (Normal) (G1 Evacuation Pause) 27M->0M(48M) 1.524ms
Fixed run, sessions still held: 0

What this code does

  • Every pause takes the heap from 27 MB back to 0 MB, the pauses are about 20 ms apart and take under 2 ms, and there are only 14 of them.
  • Nothing grows from one collection to the next: whatever the program creates for an order is freed after that order.

Example 6 · The leaking log: the floor rises

Terminal
java -Xmx48m -Xlog:gc SessionLeakDemo
Sample output
[0.014s][info][gc] Using G1
[0.219s][info][gc] GC(0) Pause Young (Normal) (G1 Evacuation Pause) 23M->2M(48M) 30.654ms
[0.241s][info][gc] GC(1) Pause Young (Normal) (G1 Evacuation Pause) 27M->5M(48M) 3.355ms
[0.260s][info][gc] GC(2) Pause Young (Normal) (G1 Evacuation Pause) 29M->8M(48M) 2.660ms
[0.280s][info][gc] GC(3) Pause Young (Normal) (G1 Evacuation Pause) 33M->11M(48M) 2.522ms
[0.299s][info][gc] GC(4) Pause Young (Normal) (G1 Evacuation Pause) 36M->13M(48M) 2.323ms
[0.318s][info][gc] GC(5) Pause Young (Normal) (G1 Evacuation Pause) 38M->16M(48M) 2.352ms
[0.338s][info][gc] GC(6) Pause Young (Normal) (G1 Evacuation Pause) 40M->19M(48M) 2.398ms
...
[0.370s][info][gc] GC(8) Pause Young (Normal) (G1 Evacuation Pause) 41M->24M(48M) 1.962ms
[0.383s][info][gc] GC(9) Pause Young (Concurrent Start) (G1 Evacuation Pause) 40M->26M(48M) 1.929ms
[0.383s][info][gc] GC(10) Concurrent Mark Cycle
[0.390s][info][gc] GC(10) Pause Remark 34M->34M(48M) 0.606ms
[0.390s][info][gc] GC(10) Pause Cleanup 35M->35M(48M) 0.056ms
[0.391s][info][gc] GC(10) Concurrent Mark Cycle 7.559ms
...
[0.614s][info][gc] GC(79) Pause Young (Normal) (G1 Evacuation Pause) 46M->45M(48M) 0.520ms
[0.616s][info][gc] GC(80) Pause Young (Concurrent Start) (G1 Evacuation Pause) 46M->45M(48M) 1.101ms
[0.616s][info][gc] GC(81) Concurrent Mark Cycle
[0.624s][info][gc] GC(82) Pause Young (Normal) (G1 Evacuation Pause) 46M->45M(48M) 0.579ms
[0.626s][info][gc] GC(83) Pause Young (Normal) (G1 Evacuation Pause) 46M->45M(48M) 0.553ms
[0.626s][info][gc] GC(81) Pause Remark 45M->45M(48M) 0.490ms
[0.627s][info][gc] GC(81) Pause Cleanup 45M->45M(48M) 0.009ms
[0.627s][info][gc] GC(81) Concurrent Mark Cycle 11.460ms
[0.628s][info][gc] GC(84) Pause Young (Normal) (G1 Evacuation Pause) (Evacuation Failure) 46M->46M(48M) 0.488ms
[0.631s][info][gc] GC(85) Pause Full (G1 Compaction Pause) 46M->45M(48M) 2.727ms
[0.633s][info][gc] GC(86) Pause Young (Concurrent Start) (G1 Evacuation Pause) (Evacuation Failure) 46M->46M(48M) 0.562ms
[0.633s][info][gc] GC(88) Concurrent Mark Cycle
[0.637s][info][gc] GC(87) Pause Full (G1 Compaction Pause) 46M->45M(48M) 4.092ms
[0.637s][info][gc] GC(88) Concurrent Mark Cycle 4.184ms
Leaky run, sessions still held: 450

What this code does

  • The heap after GC climbs 2, 5, 8, 11 … 45 MB, marking cycles start again and again, and by the end pauses are only 1 to 2 ms apart while freeing about 1 MB each.
  • Then come "Evacuation Failure" and two Full pauses that free almost nothing. This run reached GC(88), against GC(13) for the fixed run, for the same work. A few more sessions and it would end with OutOfMemoryError.

A leaking log shows the floor rising: each pause frees less, the heap after GC climbs towards the maximum, collections come closer together, marking cycles run again and again, and finally Full pauses appear. Soon after that, the program fails with OutOfMemoryError, or, like the example below, it only just finishes. The example below is an online store with a bug: it keeps every user's session after the order is delivered. Run once with the fix (fixed) and once without, both with a 48 MB heap.

Next step

Check your understanding

12 quick questions