JVM Tuning: Memory and GC Optimization

The Java Virtual Machine (JVM) provides high-level memory management via Garbage Collection (GC), which must be tuned to balance Throughput vs. Latency.

1. Garbage Collector Selection

CollectorTargetUse Case
G1GCMixedDefault for most apps; handles heaps 4GB - 64GB well.
ZGCLow LatencyUltra-low pauses (<1ms) even on TB-sized heaps.
ParallelGCThroughputBatch jobs where pause time doesn't matter, only total speed.

2. Heap Sizing and Memory Areas

3. Concrete Tuning Scenarios

Scenario A: Low Latency API (Java 17+)

Use ZGC. It uses colored pointers and load barriers to perform compaction concurrently with application threads.

Scenario B: Standard Web App

Use G1GC. Focus on the pause time goal.

4. Diagnostics: The GC Log

You cannot tune what you cannot see.


See Also: