GC Tuning and JVM Flags
JVM tuning is about finding the right balance between throughput, latency, and memory footprint for your specific workload. Here’s how to do it with real flags and analysis.
JVM Flag Cheat Sheet
Memory Sizing
| Flag | Description | Example |
|---|---|---|
-Xms |
Initial heap size | -Xms2g |
-Xmx |
Maximum heap size | -Xmx4g |
-Xss |
Thread stack size | -Xss512k |
-XX:MaxMetaspaceSize |
Metaspace limit | -XX:MaxMetaspaceSize=256m |
-XX:MaxDirectMemorySize |
Off-heap NIO buffer limit | -XX:MaxDirectMemorySize=512m |
GC Selection
| Flag | GC |
|---|---|
-XX:+UseSerialGC |
Serial |
-XX:+UseParallelGC |
Parallel (throughput) |
-XX:+UseG1GC |
G1 (default Java 9+) |
-XX:+UseZGC |
ZGC (low latency) |
-XX:+UseShenandoahGC |
Shenandoah (OpenJDK) |
G1 Tuning
| Flag | Description | Default |
|---|---|---|
-XX:MaxGCPauseMillis |
Target pause time | 200ms |
-XX:G1HeapRegionSize |
Region size (1–32MB, power of 2) | Auto |
-XX:InitiatingHeapOccupancyPercent |
Old gen % to trigger concurrent cycle | 45 |
-XX:G1NewSizePercent |
Min young gen as % of heap | 5 |
-XX:G1MaxNewSizePercent |
Max young gen as % of heap | 60 |
ZGC Tuning
| Flag | Description |
|---|---|
-XX:+UseZGC |
Enable ZGC |
-XX:+ZGenerational |
Generational ZGC (Java 21+, recommended) |
-XX:SoftMaxHeapSize |
Soft limit — ZGC tries to stay below this |
General Tuning
| Flag | Description |
|---|---|
-XX:NewRatio=2 |
Old:Young ratio (2 means Old is 2× Young) |
-XX:SurvivorRatio=8 |
Eden:Survivor ratio |
-XX:MaxTenuringThreshold=15 |
GC cycles before promotion to Old |
-XX:+DisableExplicitGC |
Ignore System.gc() calls |
-XX:+AlwaysPreTouch |
Pre-touch heap pages at startup |
GC Logging
Always enable GC logging in production — it has negligible overhead.
Java 11+ (Unified Logging)
java -Xlog:gc*:file=gc.log:time,uptime,level,tags:filecount=5,filesize=100m \
-jar app.jar
What the Log Shows
[2026-04-17T10:15:32.456+0000] GC(42) Pause Young (Normal) (G1 Evacuation Pause)
[2026-04-17T10:15:32.456+0000] GC(42) Eden: 256M -> 0M Survivors: 32M -> 32M Old: 512M -> 520M
[2026-04-17T10:15:32.462+0000] GC(42) Pause Young (Normal) 800M->552M(2048M) 6.234ms
Reading this:
- GC(42): 42nd GC event
- Pause Young: Minor GC (young generation)
- 800M→552M(2048M): Heap went from 800MB to 552MB, total heap 2048MB
- 6.234ms: Pause duration
Analyzing GC Logs
GCEasy (Online)
- Upload your
gc.logto gceasy.io - Get instant analysis: pause times, throughput, heap usage graphs
- Recommendations for tuning
GCViewer (Desktop)
# Download and run
java -jar gcviewer.jar gc.log
Key Metrics to Watch
| Metric | Healthy | Problematic |
|---|---|---|
| GC throughput | > 95% | < 90% |
| Max pause time | < 200ms (G1) | > 500ms |
| Full GC frequency | 0 or rare | Regular |
| Heap after GC | Stable baseline | Growing (leak!) |
| Allocation rate | Steady | Spiky |
Common Tuning Scenarios
Scenario 1: High Throughput (Batch Processing)
java -XX:+UseParallelGC \
-Xms8g -Xmx8g \
-XX:ParallelGCThreads=8 \
-XX:+AlwaysPreTouch \
-jar batch-job.jar
Goal: Maximize CPU time on app work. Pauses acceptable.
Scenario 2: Low Latency (API Service)
java -XX:+UseZGC \
-XX:+ZGenerational \
-Xms4g -Xmx4g \
-XX:+AlwaysPreTouch \
-jar api-service.jar
Goal: Sub-millisecond GC pauses. P99 latency matters.
Scenario 3: Balanced (Typical Web App)
java -XX:+UseG1GC \
-Xms2g -Xmx2g \
-XX:MaxGCPauseMillis=100 \
-XX:+AlwaysPreTouch \
-Xlog:gc*:file=gc.log:time:filecount=5,filesize=50m \
-jar webapp.jar
Goal: Good latency + throughput. G1 auto-tunes most parameters.
Scenario 4: Container (Limited Resources)
java -XX:+UseG1GC \
-XX:MaxRAMPercentage=75.0 \
-XX:+AlwaysPreTouch \
-jar app.jar
In containers, use
-XX:MaxRAMPercentageinstead of-Xmx— it adapts to container memory limits.
Tuning Process
1. Measure → Enable GC logging, collect baseline
2. Analyze → GCEasy/GCViewer — identify bottleneck
3. Hypothesize → "Pause times are high because Old Gen is too large"
4. Change ONE → Adjust one flag at a time
5. Measure → Compare before/after
6. Repeat → Until goals met
Golden rule: Change ONE thing at a time. Measure before and after.
Common Mistakes
| Mistake | Problem | Fix |
|---|---|---|
| Tuning without data | Guessing doesn’t work | Always start with GC logs |
| Changing many flags at once | Can’t tell what helped | One change at a time |
Setting -Xmx > physical RAM |
OS swap kills performance | Leave room for OS + Metaspace |
Using -XX:+UseCompressedOops with > 32GB heap |
Flag is ignored | Keep heap ≤ 32GB or accept larger pointers |
Not using -XX:+AlwaysPreTouch |
First access to heap pages is slow | Use it in production |
Key Takeaways
- Always log GC — negligible overhead, invaluable data.
- Start with defaults (G1), tune only when you have data.
-Xms=-Xmxin production — avoid heap resizing.- Use
-XX:MaxRAMPercentagein containers. - ZGC for latency-critical, Parallel for throughput, G1 for balanced.
- Change one flag at a time, measure before and after.
What’s Next?
Next: Memory Leaks and How to Find Them — detecting and fixing leaks in production Java apps.