ProLesson 13.3Intermediate3 min readUpdated 31 October 2026
JVM Memory Management: G1GC vs ZGC & Container Limits
Master JVM tuning for microservices, G1GC vs Generational ZGC latency profiles, container cgroup memory limits, and OOM killer prevention.
Running Java microservices inside containers requires a deep understanding of how the HotSpot JVM interacts with Linux cgroups and garbage collection algorithms.
Mismatched memory configurations are the number one cause of silent container restarts (Exit Code 137: OOMKilled) in production Kubernetes clusters.
01JVM Garbage Collectors: Latency vs Throughput
1. G1GC (Garbage-First Collector)
Default since Java 9. Designed for multi-gigabyte heaps with balanced throughput and latency.
- Mechanism: Splits heap into thousands of equal-sized regions. Prioritizes collecting regions containing the most garbage first.
- Pause Target: Supports soft target pause times via
-XX:MaxGCPauseMillis=200. - Latency Profile: Minor GC pauses typically 20ms to 100ms. Heavy load can trigger Stop-The-World full GC pauses lasting seconds.
2. Generational ZGC (Java 21+)
A scalable low-latency garbage collector designed for sub-millisecond pause times regardless of heap size.
- Mechanism: Uses colored 64-bit pointers and load barriers. Performs marking, evacuation, and pointer remapping concurrently with active application worker threads.
- Pause Target: Guaranteed sub-millisecond (< 1ms) Stop-The-World pauses.
- Java 21 Flag:
bash
-XX:+UseZGC -XX:+ZGenerational
Loading the rest of the lesson…