← Recipes

阅读中文版本

Tune JVM G1 Heap Regions

Adjust G1 region size and pause target to cut GC pauses on a heap that is too large for the default region count.

~45 minperformancejavajvm

Task

Tune G1 region size and pause goals to reduce GC pause time on a large heap.

When to use this

A service with a large heap suffers long GC pauses under load, and default region sizing causes too few regions.

Prerequisites

  • A JVM 17+ service
  • JVM GC logging enabled
  • A representative load test

Steps

1. Enable GC logging and review pause statistics.

java -Xlog:gc*:file=gc.log:time,uptime -XX:+UseG1GC -jar app.jar

Expected output: gc.log records pause times, region counts, and evacuation statistics.

2. Set region size and pause target explicitly.

java -XX:+UseG1GC -XX:G1HeapRegionSize=32m -XX:MaxGCPauseMillis=200 \
  -Xms4g -Xmx4g -jar app.jar

Expected output: With a 4 GB heap the region size yields a healthy region count and pauses trend toward the target.

Troubleshooting

Problem

Pauses still long

Solution

Check for humongous allocations larger than half a region and reduce expected maximum object size.

Problem

Throughput drops

Solution

Relax the pause target; an aggressive target increases GC frequency.

Limitations

  • Tuning depends on allocation patterns.
  • Region size changes are heap-size dependent.
  • Does not fix leaks or object churn.

What this recipe does

Tune G1 region size and pause goals to reduce GC pause time on a large heap.

A service with a large heap suffers long GC pauses under load, and default region sizing causes too few regions.

Prerequisites

  • A JVM 17+ service
  • JVM GC logging enabled
  • A representative load test

Procedure

  1. Enable GC logging and review pause statistics.
java -Xlog:gc*:file=gc.log:time,uptime -XX:+UseG1GC -jar app.jar

Expected result: gc.log records pause times, region counts, and evacuation statistics.

  1. Set region size and pause target explicitly.
java -XX:+UseG1GC -XX:G1HeapRegionSize=32m -XX:MaxGCPauseMillis=200 \
  -Xms4g -Xmx4g -jar app.jar

Expected result: With a 4 GB heap the region size yields a healthy region count and pauses trend toward the target.

How to confirm it worked

Run the commands above and confirm every expected result in order. If a step does not produce the stated output, use the troubleshooting notes before continuing. A passing run shows pause p99 falls below the target and throughput is not degraded across the load test.

Troubleshooting

Pauses still long — Check for humongous allocations larger than half a region and reduce expected maximum object size.

Throughput drops — Relax the pause target; an aggressive target increases GC frequency.

Limitations

  • Tuning depends on allocation patterns.
  • Region size changes are heap-size dependent.
  • Does not fix leaks or object churn.