Fix a Linux server that runs out of memory

We'll check memory with free, identify top processes, add swap, and 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 5
Show full guide

Steps

Goal: Check memory, identify top processes, add swap, and tune limits.

  • Run free -h to check memory and swap.
  • Good: You see usage. Proceed to Add swap or Identify processes.
  • Bad: Confirm you have the right server.

Check memory

Goal: See current memory and swap usage.

  • free -h shows Mem and Swap. ps aux --sort=-%mem | head -20 or top shows top consumers.
  • Good: You know what uses memory. Proceed to Add swap or restart/limit processes.
  • Bad: Check OOM logs in journalctl.

Add swap

Goal: Add swap if missing or too small.

  • fallocate -l 2G /swapfile, chmod 600, mkswap, swapon. Add to /etc/fstab.
  • Good: Swap active. Retest.
  • Bad: Confirm fallocate succeeded and swapon ran.

When to escalate

Escalate if:

  • OOM keeps killing critical services.
  • You cannot add swap or limit processes.
  • The workload needs more physical RAM.

Provide free -h, top/ps output, and OOM logs.

Verification

  • free -h shows available memory and swap.
  • No OOM killer activity in journalctl.
  • Services run without being killed.

Escalation ladder

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

  1. Check free and top Identify memory and swap usage; top processes.
  2. Add swap Create swap file; swapon; add to fstab.
  3. Restart or limit Restart memory-heavy services; set MemoryMax in systemd.
  4. Escalate Provide free -h, top output, OOM logs; consider more 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
  • ps aux --sort=-%mem or top output
  • journalctl OOM logs
  • Steps already tried

Does free -h show low available memory and little or no swap?

Check Mem available and Swap.

Run `free -h`. Low Mem available, no/little swap: add swap file. Swap full: processes using too much—identify with top, restart or limit.

You can change your answer later.

Add or increase swap

Create swap file: fallocate -l 2G /swapfile, chmod 600, mkswap, swapon. Add to fstab. Retest.

Identify top memory users

Run `ps aux --sort=-%mem | head -20`. Restart memory-heavy services. Set MemoryMax in systemd if needed. If OOM keeps killing, escalate.

You can change your answer later.

Set limits or escalate

systemctl edit servicename, add MemoryMax. Restart. If still OOM, escalate with free -h, top, OOM logs.

Done

Memory usage is acceptable. Monitor with free -h.

Reviewed by Blackbox Atlas

Frequently asked questions

Why would a Linux server run out of memory?
Too many processes, memory leaks, insufficient RAM or swap, or a single process using too much. Check free and top first.
Can I fix OOM myself?
Yes. Add swap, restart memory-heavy processes, set memory limits with systemd. If the workload needs more RAM, add physical memory or scale horizontally.
When should I escalate memory issues?
If you cannot add swap or limit processes, the OOM killer keeps killing critical services, or you need to add physical RAM.

Rate this guide

Was this helpful?

Thanks for your feedback.

Continue to