Video in progress

Strong Middle ⚙️ JVM and memory

What causes a Full GC in G1, and how do you tell each cause apart in the log?

For G1, a Full GC is a sign that the concurrent work didn't keep up. The main causes: no space left after an evacuation failure, no contiguous region found for a humongous object, metaspace filling up, and a System.gc() call. Each is written in the log in parentheses under its own name.

📖 Answer

The Full GC line in the G1 log tells you its own cause:

Pause Full (G1 Compaction Pause) ...        # no space left in the heap
Pause Full (Metadata GC Threshold) ...       # metaspace threshold
Pause Full (System.gc()) ...                 # someone called it explicitly
Pause Full (Heap Dump Initiated GC) ...      # jcmd/jmap took a dump

What to do for each:

  • G1 Compaction Pause / Allocation Failure: usually preceded by Evacuation Failure or "Humongous" allocations. First I check the post-GC heap trend (a leak?), then increase the heap, lower IHOP or get rid of the humongous objects.
  • Metadata GC Threshold: the metaspace growth threshold. If it happens once or twice at startup, raising the initial threshold with -XX:MetaspaceSize=256m is enough. If it keeps repeating, it is a class leak.
  • System.gc(): code or a library. I find the caller not with -Xlog:gc but from the jdk.GarbageCollection + jdk.SystemGC events in JFR. A quick fix: -XX:+ExplicitGCInvokesConcurrent.
  • Heap Dump Initiated: a monitoring agent or someone taking a dump by hand. This shows up on prod too.

Since Java 10, G1's Full GC is multi-threaded, but it is still fully stop-the-world. On an 8 GB heap it can take several seconds, so the practical goal of tuning G1 is a Full GC count close to zero.