Sub topic 15
OOMKilled and memory limits
This chapter assumes you can read kubectl describe output and know the difference between a pod's requests and limits. It builds on the CrashLoopBackOff and exit code chapters.
What OOMKilled means
A memory limit is a hard ceiling. The Linux kernel enforces it through the container's cgroup. If the container's processes try to use more memory than the limit, the kernel kills one of them with SIGKILL. Kubernetes records the reason as OOMKilled. The abbreviation OOM stands for "out of memory".
CPU works differently. A container that hits its CPU limit is slowed down (throttled), not killed. Only memory causes a kill.
Recognizing it
An OOM kill shows up in three places:
kubectl get podshowsOOMKilledbriefly, thenCrashLoopBackOffonce the restarts repeat.kubectl describe podshowsLast State: Terminated,Reason: OOMKilled, andExit Code: 137.- The exit code is 137, which is 128 plus signal 9 (SIGKILL).
To read the termination record directly, run:
kubectl get pod web -o jsonpath='{.status.containerStatuses[0].lastState.terminated}'Logs from kubectl logs web --previous often end abruptly with no error message. SIGKILL cannot be caught, so the application has no chance to log anything before it dies.
Common causes
- The limit is lower than what the application needs at normal load.
- The application leaks memory, so usage grows until it hits the limit.
- A runtime sizes itself from the node, not the container. A JVM with a large default heap is a typical example.
- A short spike, such as loading a big file, pushes usage over the limit.
Fixing it
Measure first. If metrics-server is installed, kubectl top pod web shows current memory use. Compare it with the limit.
Then set requests and limits to match real usage:
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
containers:
- name: app
image: registry.example.com/team/web:1.4.2
resources:
requests:
cpu: 250m
memory: 256Mi
limits:
memory: 512MiSetting the memory request equal to the limit is a common choice. The scheduler then reserves the memory the container may actually use, so the node is less likely to run short.
If usage keeps climbing without levelling off, raising the limit only delays the kill. That pattern points to a leak in the application. Fix the code instead.
Checklist
| Symptom | Likely cause | Action |
|---|---|---|
| Exit code 137, reason OOMKilled | Limit too low or leak | Compare `kubectl top` with the limit, then raise the limit or fix the leak |
| Exit code 137, reason Error | Killed by something other than the OOM killer, such as a failed liveness probe | Check events for probe failures |
| Pod status Evicted | Node memory pressure | Set requests that match real usage |
Exercise
A pod restarts every few minutes. describe shows Last State: Terminated, Reason: OOMKilled, Exit Code: 137. kubectl top pod shows memory rising steadily until the restart, every time. Should you only raise the limit? Why or why not?
Show answer
No. Steadily rising memory that never levels off suggests a leak. A higher limit only postpones the kill. Raise the limit briefly if you need stability, but find and fix the leak in the application. If memory instead plateaus just above the limit, raising the limit is the right fix.