A Java pod keeps getting OOMKilled at its 2Gi limit, even though `-Xmx2g` is set and the heap graph never reaches 2 GB.

IntermediateTroubleshootingDisk, memory and CPU
Answer

Likely causes → The heap is only part of JVM memory. Metaspace, thread stacks, code cache, direct buffers (Netty), and GC structures sit on top of -Xmx. A 2 GB heap in a 2 Gi container guarantees the cgroup limit is crossed.

Diagnose →

kubectl describe pod billing-7d9 | grep -A3 'Last State'
kubectl exec billing-7d9 -- jcmd 1 VM.native_memory summary   # needs -XX:NativeMemoryTracking=summary

Fix → Size the heap as a fraction of the container limit instead of a fixed -Xmx:

JAVA_TOOL_OPTIONS="-XX:MaxRAMPercentage=70 -XX:MaxDirectMemorySize=256m"

Leave 25–30% of the limit for non-heap memory, or raise the limit.

Prevent → Standardise JVM flags in the base image, and alert on container working set above 90% of the limit before the kill happens.

All Real-World Interview questions Roadmap: Interview readiness

More on Disk, memory and CPU