← Recipes

Predictable G1 Pauses

Give G1 a pause-time target while keeping the default collector family.

Flags

-XX:+UseG1GC-XX:MaxGCPauseMillis-Xmx

-XX:MaxGCPauseMillis tells G1 the pause-time goal to optimize toward. It is a target, not a hard guarantee, and too aggressive a value can reduce throughput or increase concurrent GC pressure.

Flag details

-XX:+UseG1GC
Explicitly selects the Garbage-First collector. Use it when you want the command line to document the collector choice instead of relying on JVM defaults.
-XX:MaxGCPauseMillis=100
Sets G1's soft pause-time target to 100 ms. G1 changes young-generation sizing, mixed collection work, and evacuation decisions to aim for this target, but it cannot guarantee it.
-Xms4g -Xmx4g
Starts and caps the heap at 4 GB. Fixed heap sizing reduces runtime resizing noise during pause analysis.
-Xlog:gc*
Records G1 young collections, concurrent marking, mixed collections, humongous allocations, and heap occupancy trends.

How G1 works

G1 splits the heap into many equal-sized regions instead of one contiguous young generation and old generation. It tracks which regions contain the most reclaimable garbage and collects a selected set of regions during evacuation pauses. Young collections copy live objects out of eden and survivor regions; mixed collections can also include old regions after concurrent marking identifies reclaimable space.

The pause target influences how much work G1 chooses for each pause. A lower target usually means smaller collection sets and more frequent GC work; a higher target gives G1 room to do more work per pause and may improve throughput.

When to use it

Use this for normal services where G1 is otherwise healthy but default pause behavior is a little too loose for your service-level objective.

Java version support

G1 is available in all cataloged releases here and became the default collector in Java 9. -XX:MaxGCPauseMillis is a soft goal used by several collectors, including G1. Use -Xlog:gc* on Java 9+ and legacy GC logging on Java 8.

Related JEPs

Examples

java -XX:+UseG1GC -XX:MaxGCPauseMillis=100 \
  -Xms4g -Xmx4g \
  -Xlog:gc*:file=logs/g1.log:time,uptime,level,tags \
  -jar app.jar

Use this for a service with a clear pause budget and enough heap headroom.

java -XX:+UseG1GC -XX:MaxGCPauseMillis=200 \
  -Xmx6g -jar app.jar

Use a looser target when throughput matters and 100 ms creates too much GC activity.

Verify

Check whether pauses actually move toward the goal and whether throughput or CPU usage gets worse.