Video in progress

Strong Middle ⚙️ JVM and memory

A pod keeps restarting with "OOMKilled", but there is no Java OutOfMemoryError in the log. How do you analyze this?

This is not the JVM but the Linux kernel's OOM killer: the container's total RSS exceeded the cgroup limit and the process was killed with SIGKILL. The heap did not exceed its limit, so the memory is outside the heap: metaspace, threads, direct buffers, malloc.

📖 Answer

A Java OutOfMemoryError is an error the JVM throws itself when it hits the heap or another JVM limit. It lands in the log, and HeapDumpOnOutOfMemoryError kicks in. OOMKilled is instant death with no Java error at all: exit code 137, the log cut off mid-line, no heap dump.

kubectl describe pod my-app-7f9c | grep -A3 "Last State"
#   Reason: OOMKilled   Exit Code: 137

My analysis steps:

  1. I add up the configuration: Xmx (or MaxRAMPercentage) + metaspace + threads × stack + code cache + direct memory. Often Xmx is set to 90% of the limit, and that alone is reason enough.
  2. I look at the RSS over time in the container metrics (container_memory_working_set_bytes). Is it stable after startup, or growing slowly?
  3. If it is growing, I restart with NMT and take a summary.diff.
  4. If NMT looks normal and RSS is growing: glibc arenas (MALLOC_ARENA_MAX=2) or a native library.

Prevention:

  • give the heap no more than 65-75% of the limit;
  • set explicit bounds with -XX:MaxMetaspaceSize and -XX:MaxDirectMemorySize, so that when memory runs out you get a clear Java error, not a silent death;
  • use -XX:+ExitOnOutOfMemoryError so the JVM doesn't linger in a half-alive state.