SelestL Solutions
Technology

How to Fix OOMKilled Pods and Memory Leaks

By SelestL • Published on
How to Fix OOMKilled Pods and Memory Leaks

When operating large-scale distributed workloads on Kubernetes clusters, running into sudden pod restarts due to memory exhaustion is a common operational crisis. Specifically, managing OOMKilled pods requires deep insight into Linux kernel memory management, cgroup boundaries, and application runtime heap configurations. Uncaught memory leaks inside container runtimes like Docker or containerd trigger the Out-Of-Memory killer instantly when requests breach hard cgroup limits.

Before optimizing container limits, engineers must understand how garbage collection interacts with operating system memory accounting. Poorly configured resource limits often exacerbate billing surprises, which can be monitored alongside infrastructure changes when you audit cloud finops waste and cut billing surprises across your clusters. Additionally, if database workloads running alongside your application run out of memory or lock up due to poor maintenance, you might need insights similar to those required when you fix PostgreSQL autovacuum freezing and wraparound in high-throughput environments.

Diagnosing Root Causes in OOMKilled Pods

When a container exceeds its memory limit, the Linux kernel terminates the process immediately with an exit code of 137. To confirm that your workload failed due to an explicit out-of-memory event rather than an application crash, execute the standard diagnostic command using kubectl.

kubectl get pods -A | grep OOMKilled
kubectl describe pod <pod-name> -n <namespace>

Review the output for Last State: Terminated with a Reason: OOMKilled flag and Exit Code: 137. This confirms the Linux cgroup subsystem intervened because the process consumed RAM beyond the designated container boundary.

Pro Tip: Never rely solely on Kubernetes event logs for OOM events because logs are often truncated or lost if the container crashes before flushing its buffer. Configure persistent cluster logging or kernel ring buffer scraping via Prometheus and node exporters to catch exact memory consumption spikes.

Step-by-Step Remediation Playbook

Resolving memory pressure issues requires a disciplined approach to workload profiling and resource allocation. Follow these operational steps to isolate and eliminate memory leaks permanently.

Step 1: Inspect Container Cgroup Memory Metrics

Log directly into the affected worker node via SSH or a privileged debug pod. Inspect the raw cgroup filesystem to see peak memory usage statistics recorded prior to the termination event.

cat /sys/fs/cgroup/memory/kubepods/burstable/pod<uid>/<container-id>/memory.max_usage_in_bytes

Step 2: Profile Application Heaps and Garbage Collection

For Node.js, Python, Java, or Go applications, memory leaks frequently stem from uncollected object references, unbounded caches, or circular dependencies. Attach a profiler to capture heap allocations under load testing environments before deploying fixes to production.

Warning: Setting Kubernetes requests and limits to identical values creates Guaranteed QoS pods, which prevents CPU throttling and eviction under normal node pressure. However, setting these limits too low without thorough load testing will cause immediate crash loops.

Step 3: Right-Size Resource Requests and Limits

Adjust your deployment YAML manifests to enforce realistic resource boundaries. Ensure that container memory requests provide adequate headroom above baseline consumption, while limits guard against runaway memory leaks.

resources:
  requests:
    memory: "1Gi"
    cpu: "500m"
  limits:
    memory: "2Gi"
    cpu: "1000m"

Diagnostic Matrix for Container Memory Failures

Symptom Exit Code Root Cause Remediation Action
OOMKilled 137 Container hit cgroup memory limit Increase limit or fix memory leak
Node Eviction N/A Node ran out of physical memory Add cluster nodes or adjust requests
JVM OutOfMemoryError 0 or 1 Heap size exceeded JVM max setting Tune -Xmx parameters relative to cgroups

Need Expert Kubernetes Architecture Support?

If your engineering team is battling chronic cluster instability, cascading pod evictions, or complex infrastructure bottlenecks, our senior platform architects are ready to help. Explore our enterprise cloud & web development services to see how we build resilient production environments. Reach out directly to contact our technical engineering desk for an infrastructure audit.

Scale Your Infrastructure with SelestL Solutions

Struggling with production performance, cloud architecture, or enterprise deployment? Our engineering team builds resilient, high-throughput systems tailored for scale.

Hardening Your Infrastructure for Production

Preventing recurrence requires implementing automated guardrails across your continuous delivery pipelines. Mandate Validated Resource Quotas at the namespace level using Kubernetes LimitRanges to ensure no developer can deploy unbounded containers. Pair these policies with vertical pod autoscalers operating in recommendation mode to gather historical usage data before enforcing strict limits.

Finally, establish comprehensive alerting rules that trigger when container memory utilization exceeds 85 percent of its assigned limit for more than five consecutive minutes. Catching memory growth early allows your team to intervene before the kernel invokes the out-of-memory killer, ensuring uninterrupted uptime for your critical customer-facing microservices.

Frequently Asked Questions

What does exit code 137 mean in Kubernetes?
Exit code 137 indicates that the container process was terminated forcefully by the Linux kernel because it exceeded its allocated memory limit cgroup boundary.
How do I find out which pod was terminated due to OOM?
Run kubectl get pods -A | grep OOMKilled to list terminated pods, and use kubectl describe pod to inspect the exact termination reason and exit code.
Should I set memory requests and limits to the exact same value?
Setting requests equal to limits creates a Guaranteed Quality of Service tier, which prevents CPU throttling and node eviction priority shuffling, but requires accurate workload sizing.
Why does Java behave differently with Kubernetes memory limits?
Older Java Virtual Machine versions were not cgroup-aware and calculated heap sizes based on underlying host memory rather than container limits, leading to unexpected OOM kills.
How can I prevent node-level memory pressure evictions?
Configure strict namespace memory quotas, use Horizontal Pod Autoscalers, and ensure worker nodes have sufficient spare capacity and correct eviction thresholds.
Tags: #Kubernetes #Docker #Linux #Troubleshooting #DevOps

Related Articles