Sub topic 7
Out-of-Memory Kills and Resource Pressure
This chapter assumes you can read docker inspect output and know that exit code 137 means a process received SIGKILL (128 + 9). Here you will learn to tell an out-of-memory (OOM) kill from other causes of 137, and to find the memory pressure behind it.
How an OOM kill happens
A container can have a memory limit, set with docker run --memory or mem_limit in Compose. Without a limit, a container can use as much host memory as the kernel allows. When the container's processes exceed the limit and the kernel cannot reclaim memory, the kernel's OOM killer sends SIGKILL to a process in that container. SIGKILL cannot be caught, so the application gets no chance to log a message or shut down cleanly. That is why logs often end abruptly.
The host can also run out of memory. In that case the kernel may kill a container that has no limit at all.
Confirming the cause
Exit code 137 only says SIGKILL was sent. Someone may have run docker kill, or a stop may have timed out. Check the OOMKilled flag to separate these cases.
docker inspect --format 'exit={{.State.ExitCode}} oom={{.State.OOMKilled}} restarts={{.RestartCount}} error={{.State.Error}}' web| exit | oom | Likely meaning |
|---|---|---|
| 137 | true | The kernel OOM killer stopped the container |
| 137 | false | `docker kill`, a stop timeout, or another SIGKILL sender |
| 0 or 1 | false | The application exited by itself, not an OOM kill |
Watching resource use
docker stats shows live usage per container. Use --no-stream for a single snapshot.
docker stats --no-streamThe MEM USAGE / LIMIT column shows current use against the limit. A container sitting near its limit is at risk. A value that climbs steadily across snapshots suggests a leak. A value that jumps at a specific moment, such as when a large request or batch job arrives, suggests a load spike.
If the limit shows the full host memory, no limit is set. Also note that docker stats counts some cache memory, so the number can look higher than the application's own heap.
Fixing the problem
Pick the fix that matches the cause:
- If the application genuinely needs more memory, raise the limit, for example
--memory 1g. - If memory grows without bound, fix the leak or cap the application's own pool sizes.
- If a runtime ignores container limits, set its own cap. For example, tune the JVM with
-XX:MaxRAMPercentage, or Node.js with--max-old-space-size. - If the host is short on memory, move containers or add memory. Raising one container's limit will not help.
docker run -d --name web --memory 512m --memory-swap 512m myimage:latestSetting --memory-swap equal to --memory disables swap for the container. The process then hits the limit sooner instead of slowing down on disk.
Exercise
A container exits with code 137, and docker inspect shows OOMKilled=false. Name two possible causes and one command that helps check for a hidden OOM kill.
Show answer
Possible causes are a manual docker kill and a stop that exceeded its timeout, after which Docker sent SIGKILL. A child process may also have been OOM-killed. Run dmesg on the host and look for "Out of memory" or "oom-kill" lines to check this.