Sub topic 5
Memory limits: OOM kills and exit code 137
This chapter assumes you can apply a pod manifest with kubectl and know the requests and limits fields. It also assumes you know the units from the earlier chapter, such as Mi and Gi.
Memory is not compressible
CPU can be slowed down, so a container over its CPU limit is throttled. Memory cannot be slowed down. Once a process holds a page of memory, the kernel can't take it back without losing data. So when a container tries to use more memory than its limit, Kubernetes ends the process.
The kubelet passes the memory limit to the Linux kernel as a control-group (cgroup) setting. When the container's usage reaches that ceiling and the kernel can't reclaim enough memory, the kernel's out-of-memory (OOM) killer sends SIGKILL to a process in the container.
The memory request works differently. It only affects scheduling and eviction order. The kernel doesn't enforce it at runtime.
What happens step by step
Exit code 137 is 128 + 9. Signal 9 is SIGKILL. The kubelet then restarts the container according to the pod's restartPolicy. After repeated kills, the pod shows CrashLoopBackOff, which means the kubelet is waiting longer between restarts.
Reproduce an OOM kill
This pod allocates memory in a loop until it passes its 64 MiB limit.
apiVersion: v1
kind: Pod
metadata:
name: oom-demo
spec:
restartPolicy: Never
containers:
- name: hog
image: python:3.12-slim
command: ["python", "-c"]
args:
- |
chunks = []
while True:
chunks.append(bytearray(10 * 1024 * 1024))
resources:
requests:
memory: 32Mi
limits:
memory: 64MiApply it, wait a few seconds, then read the termination details:
kubectl apply -f oom-demo.yaml
kubectl get pod oom-demo -o jsonpath='{.status.containerStatuses[0].lastState.terminated.reason}'
kubectl get pod oom-demo -o jsonpath='{.status.containerStatuses[0].state.terminated.exitCode}'With restartPolicy: Never the container doesn't restart, so the result is in state.terminated. The reason is OOMKilled and the exit code is 137. In a Deployment the pod restarts, so the same data moves to lastState.terminated.
Common errors
- Reading 137 as proof of an OOM kill. Any
SIGKILLproduces 137. This includes a container that ignoredSIGTERMand was killed after the grace period. Always checkreason: OOMKilledbefore you change memory settings. - A limit that matches only the application heap. A JVM with
-Xmx512min a 512Mi container will be killed. Metaspace, thread stacks and native buffers add to the total. Leave headroom above the heap. - Forgetting that some memory is not heap. Page cache and files written to a
tmpfsvolume, such asemptyDirwithmedium: Memory, count toward the container's memory. - Setting no limit. The container can then grow until the node runs low on memory, and other pods may be evicted. Eviction is covered in a later chapter.
- A child process killed instead of the main one. The kernel may kill a worker process while PID 1 survives. The container keeps running, but you will see errors in its logs and
OOMKilledmay not appear in the pod status.
Checking the evidence
kubectl describe pod oom-demo shows the Last State block with Reason: OOMKilled and Exit Code: 137. Compare that against the limit in the same output to decide whether the limit is too low or the application is misbehaving.
Exercise
A pod shows exit code 137 but its reason is Error, not OOMKilled. A memory limit is set. What is the likely cause, and what should you check?
Show answer
Something other than the kernel OOM killer sent SIGKILL. A common cause is a container that did not stop within the termination grace period after SIGTERM, such as during a rollout or a failed liveness probe. Check kubectl describe pod events for probe failures and "Killing" messages, and check whether the application handles SIGTERM.