A Java service running inside a Kubernetes pod with a 2GB memory limit was restarting every six minutes due to Out-of-Memory (OOMKilled) errors. Even after doubling the limit, the pod continued to die.
This guide walks through a systematic diagnosis tree:
- Understanding cgroup memory accounting and what the kernel actually counts
- Examining JVM off-heap components such as Metaspace, direct buffers, and thread stacks
- Using a kubectl command sequence to narrow down the root cause in under ten minutes
- Implementing fix patterns that hold in production
- Post-incident prevention strategies
The key insight: OOMKilled often stems from off-heap memory consumption that isn't captured by standard heap monitoring tools. By inspecting cgroup memory stats and tuning JVM flags, you can eliminate recurring restarts.