The Java Virtual Machine (JVM) provides high-level memory management via Garbage Collection (GC), which must be tuned to balance Throughput vs. Latency.
| Collector | Target | Use Case |
|---|---|---|
| G1GC | Mixed | Default for most apps; handles heaps 4GB - 64GB well. |
| ZGC | Low Latency | Ultra-low pauses (<1ms) even on TB-sized heaps. |
| ParallelGC | Throughput | Batch jobs where pause time doesn't matter, only total speed. |
-Xms, -Xmx): The main object area. Expert Rule: Set -Xms and -Xmx to the same value to avoid the performance jitter caused by JVM heap resizing.-Xmn): Where new objects are born. High allocation rates require a larger Young Gen to prevent premature promotion to the Old Gen.-XX:MaxMetaspaceSize): Stores class metadata. If your app dynamically loads many classes, increase this to avoid OutOfMemoryError: Metaspace.Use ZGC. It uses colored pointers and load barriers to perform compaction concurrently with application threads.
-XX:+UseZGC -Xmx16G -XX:SoftMaxHeapSize=12GUse G1GC. Focus on the pause time goal.
-XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:InitiatingHeapOccupancyPercent=45You cannot tune what you cannot see.
-Xlog:gc*:file=gc.log:time,uptime,level,tagsSee Also: