JVM Tuning: Flags That Matter

JVM Tuning: Flags That Matter in Java: Most JVM tuning advice is obsolete or cargo-culted. Modern JVMs choose good defaults, and the honest starting position…

Concept Explanation

Most JVM tuning advice is obsolete or cargo-culted. Modern JVMs choose good defaults, and the honest starting position is: set the heap, pick a collector, measure, and change nothing else without evidence.


Heap Sizing

java -Xms512m -Xmx2g -jar app.jar
  • -Xms — initial heap
  • -Xmx — maximum heap

Setting them equal is standard for servers: the JVM never pauses to grow the heap, and memory usage becomes predictable.

In a container, use percentages instead of fixed values so the setting survives a limit change:

java -XX:MaxRAMPercentage=75.0 -jar app.jar

Never set the heap to 100% of the container limit — the JVM also needs metaspace, thread stacks, code cache, and native buffers. Exceed the limit and the kernel kills the process with no Java-level error at all.


Choosing a Collector

CollectorFlagBest for
G1 (default)-XX:+UseG1GCmost applications
Parallel-XX:+UseParallelGCbatch jobs, throughput over latency
ZGC-XX:+UseZGClarge heaps, sub-millisecond pauses
Serial-XX:+UseSerialGCtiny heaps, single-core containers

G1 is the right default. Move to ZGC when pause times genuinely matter and your heap is large; move to Parallel when total throughput matters and pauses do not.

# Give G1 a pause-time goal rather than micromanaging generations
java -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -jar app.jar

Observability Flags

These are safe to enable everywhere and invaluable when something goes wrong:

java -Xlog:gc*:file=gc.log:time,uptime:filecount=5,filesize=10M \
     -XX:+HeapDumpOnOutOfMemoryError \
     -XX:HeapDumpPath=/var/log/app/ \
     -jar app.jar

GC logs cost almost nothing and answer most memory questions after the fact.


Reading a GC Log

[2.451s] GC(3) Pause Young (Normal) 512M->48M(2048M) 12.3ms
          |                          |     |    |      |
          collection number    before after total  pause

Signs of health: young collections dominate, pauses are short, and the after value returns to a stable baseline.

Signs of trouble: frequent Full GC entries, pauses in the hundreds of milliseconds, or an after value that climbs on every cycle (a leak).


Thread Stacks

java -Xss512k -jar app.jar

The default is around 1 MB per thread. With 500 threads that is 500 MB of native memory, outside the heap. Reducing it helps high-thread-count servers — but too small a stack means StackOverflowError on deep recursion.


Common Mistakes

  1. Copying flags from a blog post: Many recommendations predate G1 and actively hurt on modern JVMs.
  2. Tuning before measuring: Without a GC log or profile, you are guessing.
  3. Heap equal to the container limit: Guarantees an eventual OOM kill.
  4. Fixed -Xmx in a container: Ignores the limit the orchestrator granted.
  5. Optimising GC when the real problem is allocation: Allocating less always beats collecting faster.

Best Practices

  • Start with heap size and collector only. Add flags only in response to a measurement.
  • -Xms = -Xmx for servers; MaxRAMPercentage in containers.
  • Always log GC in production — it is nearly free and often decisive.
  • Load test with production-like data volumes; GC behaviour changes completely with heap pressure.
  • Fix allocation hot spots first: A profiler pointing at excessive object creation is worth more than any flag.

Practice Exercises

  1. The Log: Run an allocation-heavy program with GC logging enabled and identify the young-collection pause times.
  2. The Collector Comparison: Run the same workload under G1 and Parallel GC, and compare total pause time against total throughput.
  3. The Container Math: Given a 2 GiB container limit, choose a MaxRAMPercentage and justify the non-heap headroom you left.