OOMKilled in Kubernetes: Understanding Exit Code 137 and How to Fix It
You deploy a service, it runs fine for a while, then the pod disappears and restarts. When you check the status, you see OOMKilled and exit code 137. This is one of the most common Kubernetes production issues, and it almost never means what it looks like at first glance.
Simply raising the memory limit and moving on is the wrong response. This guide covers what OOMKilled actually means, how to diagnose the root cause, and how to prevent it from recurring.
What OOMKilled Means
OOM stands for Out Of Memory. When a container exceeds its configured memory limit, the Linux kernel sends it a SIGKILL signal to terminate the process immediately. There is no graceful shutdown, no cleanup, just termination.
Exit code 137 is the signal for this: it equals 128 plus SIGKILL (signal 9), which is the standard Linux encoding for killed processes. When you see exit code 137 in Kubernetes, the container did not crash due to an application error. The kernel killed it deliberately.
Kubernetes does this to protect the node. If one container were allowed to consume unlimited memory, it would eventually starve other pods and potentially destabilize the entire node.
Confirming the Issue
Start by describing the pod to confirm OOMKilled is the actual cause:
kubectl describe pod <pod-name> -n <namespace>
Look at the Last State section in the output. You should see something like:
Last State: Terminated
Reason: OOMKilled
Exit Code: 137
Check the current resource configuration to see what limits are set:
kubectl get pod <pod-name> -o yaml | grep -A 6 resources
Then check actual current usage to compare against the limits:
kubectl top pod <pod-name>
Examining Previous Container Logs
Because the container was killed, its current logs may be sparse. The most useful information is in the logs from the previous instance before it was terminated:
kubectl logs <pod-name> --previous
Look for signs of what the container was doing just before it was killed. Memory leaks often leave traces in the form of growing object counts, repeated allocation warnings, or processing of unexpectedly large inputs.
Common Root Causes
Memory leaks: The most common cause. Objects accumulate in memory faster than the garbage collector can reclaim them. The symptom is gradual memory growth over hours or days until the limit is hit. If your pod runs fine on restart but fails again after running for a while, this is likely the cause.
Processing large files in memory: Code that reads an entire file into memory before processing it will allocate as much memory as the file size. A 500MB file loaded fully into a container with a 256Mi limit will immediately trigger OOMKilled. Use streaming or chunked processing instead.
Loading full datasets before processing: Similar to large files, database queries that return unbounded result sets can consume far more memory than expected. Always paginate or use cursors for large data operations.
Traffic spikes: Each incoming request may create connections, sessions, and in-memory objects. A sudden spike in traffic can multiply memory usage faster than the autoscaler responds. If OOMKilled correlates with traffic peaks, this is likely the cause.
Misconfigured limits: Sometimes the limit is simply set too low relative to what the application actually needs under normal operation. This is worth checking, but rule out the causes above first before concluding the limit needs to be raised.
Checking Recent Deployment History
If OOMKilled started happening after a recent deployment, the change itself may have introduced the problem:
kubectl rollout history deployment <deployment-name>
Compare memory usage before and after the problematic release. A new dependency, a configuration change, or a feature that processes larger payloads can all shift memory usage significantly.
Prevention Strategies
Set resource requests and limits based on observed usage, not guesses. Run your application under realistic load, measure actual memory consumption with kubectl top, and set limits with reasonable headroom above the observed peak.
Use streaming and batch processing for any operation that handles large inputs. Never load more data into memory than you need at one time.
Set up monitoring and alerting on memory usage before it reaches the limit. A Prometheus alert at 80% memory utilization gives you time to investigate before the container is killed.
Load test new features before deploying to production, specifically focusing on memory behavior under sustained load. Many memory leaks only appear after minutes or hours of continuous operation.
Review your application's dependency list when upgrading libraries. Third-party packages can change their memory behavior between versions in ways that are not obvious from changelogs.
Conclusion
OOMKilled and exit code 137 tell you that your container was killed by the kernel for exceeding its memory limit. The real question is why it exceeded the limit. A structured diagnosis, examining previous logs, comparing limits against observed usage, checking deployment history, and understanding the workload characteristics, consistently produces better outcomes than simply raising the memory limit and hoping the problem goes away.
Memory issues that go undiagnosed tend to return, often at the worst possible time.