Interview Prep/Kubernetes/Resources, OOM, probes and termination

A Java service is OOMKilled at 2 GiB limit but the heap is only `-Xmx1800m`.

IntermediateTroubleshootingResources, OOM, probes and termination
Answer

Likely causes → The JVM uses much more than the heap: metaspace, thread stacks, code cache, direct buffers (Netty), GC structures. Heap 1.8 GiB plus 400+ MiB of native memory exceeds 2 GiB.

Diagnose →

kubectl exec api-x -- jcmd 1 VM.native_memory summary   # needs -XX:NativeMemoryTracking=summary
kubectl exec api-x -- jcmd 1 GC.heap_info

Fix → Size the heap as a percentage of the cgroup limit instead of a fixed value:

env:
  - name: JAVA_TOOL_OPTIONS
    value: "-XX:MaxRAMPercentage=70 -XX:MaxDirectMemorySize=256m"

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

Prevent → Use container-aware JVMs (JDK 17+), percentage-based sizing, and load-test at the production limit.

All Kubernetes questions Roadmap: Kubernetes core

More on Resources, OOM, probes and termination