Java Core & JVM (50 Qs)•Part 3: JVM Internals, Low-Latency Systems & Production Tuning (25 - 35)
How do you tune JVM flags for containerized microservices in Docker and Kubernetes?
senior LevelMost AskedCode ExampleDocker & ContainersKubernetesMicroservices
In Simple Words: Historically, the JVM inspected the host physical machine's RAM and CPU cores rather than the Linux cgroups limits set by Docker or Kubernetes, causing pods to be killed by the Linux OOM Killer.
Essential Container Flags:
bash
java -XX:+UseContainerSupport \
-XX:MaxRAMPercentage=75.0 \
-XX:InitialRAMPercentage=50.0 \
-XX:+UseZGC -XX:+ZGenerational \
-XX:+ExitOnOutOfMemoryError \
-jar app.jar
Why MaxRAMPercentage=75% (Not 100%):
The JVM process consumes memory beyond the heap:
- Metaspace (loaded class definitions)
- Thread Stacks (1MB per platform thread)
- Direct Byte Buffers / Off-Heap Memory (Netty / NIO)
- JIT Code Cache
Allocating 100% to heap guarantees that the container will breach Kubernetes limits and trigger
OOMKilled(Exit Code 137).