Fix out of memory kills (OOM) on a Linux server
An out of memory kill happens when the kernel ends a process because the server ran out of RAM and swap; the kernel log names the victim and you fix it by freeing, limiting or adding memory.
- Updated Nov 27, 2026
- Markdown
Confirm an OOM kill in the kernel log, find the process using the memory and fix it with limits, swap or a smaller footprint.
- SSH access to the server with sudo
A service disappears without an error, a container restarts with exit code 137, or the whole server becomes unresponsive for a while and then recovers. On Linux, that pattern often means the kernel ran out of memory and ended a process to save the machine. The kernel log confirms it and names the victim. After that, the fix is to use less memory, cap the hungry process, or give the server more room.
How it works#
Linux lets programs ask for more memory than physically exists, on the bet that not all of it is used at once. When RAM and swap are both nearly gone and the kernel cannot reclaim cache, it runs the OOM killer. The OOM killer scores processes, mostly by how much memory they use, and kills a victim so the rest survive. The victim is not always the process that caused the problem: it is the one the kernel thinks frees the most memory.
Containers add a second path. A container can have a memory limit set by Docker through cgroups. If the container’s processes exceed that limit, the kernel kills them inside that cgroup even when the host has free memory. Docker records this as OOMKilled. See Docker restart policies for why a killed container then comes back.
Step 1: Confirm it was an OOM kill#
sudo journalctl -k | grep -i -E 'out of memory|oom'The older command is sudo dmesg | grep -i 'out of memory'. A hit looks like Out of memory: Killed process 4121 (node) ... followed by the process name, PID and memory sizes. For a container limit you may see Memory cgroup out of memory instead. Note the time. If nothing shows up, the kernel log may have rotated, or the process died for another reason, such as a crash or a manual stop. Reading more in read logs with journalctl helps.
Step 2: Check the container flag#
docker inspect --format 'oom={{.State.OOMKilled}} exit={{.State.ExitCode}}' <container>oom=true exit=137 means the container’s process was killed for memory. Exit code 137 alone only says SIGKILL, so check the flag. Note that the flag shows the last run only. Also look at the configured limit:
docker inspect --format '{{.HostConfig.Memory}}' <container>0 means no limit. A number is the limit in bytes.
Step 3: See current memory use#
free -hRead the available column, not free, because Linux uses spare RAM for cache that it releases when needed. If available is small and swap shows 0B, the server has no cushion. More detail is in check disk, memory and CPU usage.
Step 4: Find the memory hog#
ps aux --sort=-%mem | headThe %MEM and RSS columns show the biggest processes. Look at the top few. In top, press M to sort by memory. For containers:
docker stats --no-streamCompare memory use with the limit per container. Repeat the check over time: a process whose memory only grows has a leak, while a process that spikes during a job has a peak problem. Common hogs are a database with default settings tuned for a bigger machine, a build or image processing job running on the production server, and many worker processes each loading the same data.
Step 5: Fix it#
Choose the fix that matches what you found.
Reduce the footprint. Lower the worker count, set the app’s own heap limit (for example NODE_OPTIONS=--max-old-space-size=512), tune database memory settings, or move heavy jobs elsewhere.
Set sensible memory limits. A limit on each container stops one greedy service from taking the whole host. In Compose:
services:
app:
image: myapp:latest
mem_limit: 512mPick a value above the app’s normal peak, based on what docker stats showed. A limit that is too low causes the kills you are trying to avoid. Also keep the sum of limits below the host’s RAM.
Add swap. Swap lets the kernel move idle pages to disk, which smooths spikes. Follow add swap space on Ubuntu. Swap is far slower than RAM, so it is a safety net and not a replacement for memory. A server that swaps constantly will feel slow.
Resize the server. If the workload truly needs more than you have, a bigger plan is the honest fix.
Step 6: Restart what was killed#
A killed service may not come back by itself. Start it, then watch it:
sudo systemctl restart myapp
docker start <container>Use restart policies so the service returns after a kill, but do not rely on them to hide a repeating cause. A loop of kills is the pattern described in fix a Docker container that keeps restarting.
Check it worked#
Let the workload run through its normal peak, then check:
free -h
sudo journalctl -k --since "1 hour ago" | grep -i 'out of memory'
docker inspect --format 'oom={{.State.OOMKilled}} restarts={{.RestartCount}}' <container>You want no new kernel kill lines, oom=false, and a restart count that has stopped growing. Keep an eye on available memory over a day.
Prevent it#
- Set limits on every container and keep swap as a cushion.
- Alert when available memory drops below a fixed share of RAM.
- Run builds and one-off heavy jobs off the production server.
Edge cases and gotchas#
- Page cache looks like use:
freeshows cache as used. Judge byavailable. - Victim is not the culprit: The kernel may kill a large database while a smaller leaking process caused the pressure.
- Limits without swap: A container limit can also cap swap use, so a memory limit alone may kill sooner than expected.
- Overcommit: A process can start fine and be killed later, when it touches pages it asked for.
- Logs rotate: The kernel ring buffer is small. Use
journalctl -kfor history, and note that a reboot clears the buffer unless the journal is persistent. - Unit limits: A systemd service may have
MemoryMaxset, which kills the service in its own cgroup.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
| Out of memory: Killed process in log | Host out of RAM and swap | Reduce use, add swap, resize |
| Container exit 137, OOMKilled true | Container limit reached | Raise the limit or reduce the app’s use |
| Exit 137, OOMKilled false | Something sent SIGKILL | Check docker kill, orchestrators, scripts |
| Server freezes then recovers | Heavy swapping before the kill | Add memory, find the hog |
| Kills during builds | Build uses more than runtime | Build on another machine or in CI |
| Memory grows until a kill | Leak | Fix the leak, restart on a schedule as a stopgap |
Quick reference#
sudo journalctl -k | grep -i 'out of memory'
free -h
ps aux --sort=-%mem | head
docker stats --no-stream
docker inspect --format 'oom={{.State.OOMKilled}} exit={{.State.ExitCode}}' <container>Related lessons#
- Check disk, memory and CPU usage: read free, top and ps correctly.
- Add swap space on Ubuntu: a safety net for short spikes.
- Docker restart policies: why a killed container comes back.
- Read logs with journalctl: search kernel messages by time.
- Fix a Docker container that keeps restarting: the loop that OOM kills can cause.
Next#
Continue with Fix a deploy that passes health checks but serves errors.
Try it: Confirm and find an OOM kill
Check the kernel log for a kill, see memory use, and find the biggest process and container.
Want a real machine? Free browser labs: Killercoda
Check what you learned
Frequently asked questions
How do I know if the OOM killer ended my process?
Search the kernel log with sudo journalctl -k or sudo dmesg for the line Out of memory: Killed process. For a Docker container, docker inspect shows State.OOMKilled true.
Why did my container exit with code 137?
137 means it received SIGKILL. When docker inspect also shows OOMKilled true, the container hit its memory limit or the host ran out of memory.
Does adding swap fix out of memory problems?
It gives the kernel more room and can prevent kills during short spikes, but swap is much slower than RAM. A process that always needs more memory than you have should be fixed or the server resized.
How do I find which process uses the most memory?
Run ps aux --sort=-%mem | head, or top and press M to sort by memory. For containers, docker stats shows memory per container.
Sources
Keep going: guides and tutorials
- Docker Container Restarting in a Loop: 5 Causes, Fixed Five real reasons a Docker container keeps restarting, each reproduced with its exit code, the command that finds it and the fix. Then the same crash on Levelrail.
- Our Health Check Failed and the Deploy Went Live Anyway A postmortem from our own dogfood run: a failed readiness probe still received traffic. What broke, why two correct parts added up to a bug, and the fix.
- Roll Back a Failed Docker Deploy Without Downtime Ship a broken release on purpose, watch your users stay on the old version, then roll back with one command. Real commands, real output, self-hosted.