Fix out-of-memory error on a server

We'll confirm memory with free -h, check dmesg for OOM killer activity, identify the cause, add swap or tune limits—or tell you when to escalate.

Category
Troubleshooting · Servers & Linux
Time
15–30 min
Last reviewed
What you'll need
  • SSH or console access with sudo

Step-by-step diagnostic

Step 1 of 7
Show full guide

Steps

Goal: Confirm memory state, check OOM killer activity, and fix or escalate.

  • Run free -h to confirm memory and swap usage. Run dmesg | grep -i oom or journalctl -k | grep -i oom to see OOM killer messages.
  • Good: You see memory state and which process was killed. Proceed to Check memory and OOM.
  • Bad: No OOM in dmesg—check the exact error message; it may be a different issue.

Check memory and OOM

Goal: Understand current memory state and OOM killer activity.

  • Run free -h. Check Mem (available) and Swap (used, total). When Mem available is near zero and Swap is full or absent, the system hit memory exhaustion.
  • Run dmesg | grep -i oom or journalctl -k | grep -i oom. The OOM killer logs which process it killed. Note the process name and PID.
  • Good: You know memory state and the victim process. Proceed to Add swap or limit processes.
  • Bad: Run dmesg | tail -100 to see recent kernel messages.

Add swap or limit processes

Goal: Add swap if missing, or restart and limit leaky processes.

  • Run swapon --show. If swap is absent or too small: sudo fallocate -l 2G /swapfile, sudo chmod 600 /swapfile, sudo mkswap /swapfile, sudo swapon /swapfile. Add /swapfile none swap sw 0 0 to /etc/fstab.
  • When a single process leaks: sudo systemctl restart servicename. For long-term: set MemoryMax in the systemd unit.
  • When swap thrashing causes slowness: sudo sysctl vm.swappiness=10. Add to /etc/sysctl.conf to persist.
  • Good: Swap added or process restarted. Check free -h to confirm headroom.
  • Bad: OOM kills continue—see When to escalate.

When to escalate

Escalate if:

  • OOM kills continue after adding swap and limiting processes.
  • You cannot add swap (e.g. no disk space).
  • The workload needs more physical RAM.

Provide free -h and dmesg | grep -i oom (or journalctl OOM output) before escalating.

Verification

  • free -h shows Mem available and Swap headroom.
  • dmesg | grep -i oom shows no new OOM kills (or fewer after changes).
  • Applications run without being killed.

Escalation ladder

Work from the device outward. Stop when the problem is fixed.

  1. Check memory and OOM Run free -h and dmesg | grep -i oom.
  2. Add swap fallocate, mkswap, swapon; add to fstab.
  3. Restart or limit Restart leaky services; set MemoryMax in systemd.
  4. Tune swappiness Lower vm.swappiness to reduce thrashing.
  5. Escalate Provide free -h and dmesg OOM output; consider add RAM.

What to capture if you need help

Before calling support or posting for help, have these ready. It speeds everything up.

  • free -h output
  • dmesg or journalctl OOM messages
  • swapon --show output
  • Steps already tried

Did you see an OOM error or process killed?

Check dmesg | grep -i oom or journalctl -k | grep -i oom.

Run `free -h` and `dmesg | grep -i oom`. OOM in dmesg: kernel killed a process. No OOM: may be a different error—check the exact message. Confirm you should see memory state and OOM messages.

You can change your answer later.

Check the exact error

If the error is not OOM, check the exact message. "Cannot allocate memory" or "Killed" often means OOM. Run dmesg | tail -100 to see recent kernel messages.

Is swap absent or too small?

Run free -h and swapon --show.

Run `free -h` and `swapon --show`. No swap or small: add swap file. Swap present and adequate: check for memory leak—ps aux --sort=-%mem.

You can change your answer later.

Add swap file

`sudo fallocate -l 2G /swapfile`, `chmod 600 /swapfile`, `mkswap /swapfile`, `swapon /swapfile`. Add to /etc/fstab. Check free -h. Confirm you should see swap active.

Is a single process leaking?

ps aux --sort=-%mem | head -20

Run `ps aux --sort=-%mem | head -20`. Single process high: restart it or set MemoryMax in systemd. Many processes: add swap or RAM. Confirm you should see top memory consumers.

You can change your answer later.

Restart or limit the process

Restart: `sudo systemctl restart servicename`. Limit: add MemoryMax=512M in the unit. Confirm you should see memory freed.

Escalate

Provide free -h and dmesg OOM output. Consider adding RAM. See fix-swap-is-full or fix-linux-server-runs-out-of-memory for related steps.

Reviewed by Blackbox Atlas

Frequently asked questions

What causes out-of-memory errors on a server?
RAM and swap exhausted. Too many processes, a memory leak, or insufficient swap. The kernel OOM killer selects a process to kill to free memory. Check free -h and dmesg.
How do I see OOM killer activity?
Run dmesg | grep -i oom or journalctl -k | grep -i oom. The kernel logs which process was killed and why.
When should I escalate OOM errors?
If OOM kills continue after adding swap and limiting processes, or you need to add physical RAM. Provide free -h and dmesg OOM output.

Rate this guide

Was this helpful?

Thanks for your feedback.

Continue to