Fix swap that is full

We'll confirm usage with free -h, check swap with swapon, tune vm.swappiness, add swap if needed, and address memory pressure—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 6
Show full guide

Steps

Goal: Confirm swap usage, tune swappiness, add swap if needed, and address memory pressure.

  • Run free -h to confirm swap and memory usage. Run swapon --show to list swap devices.
  • Good: You see Swap Used, Total, and Mem. Proceed to Check swap and memory.
  • Bad: Swap not shown—check if swap is disabled.

Check swap and memory

Goal: Understand current swap and memory state.

  • Run free -h. Check Swap row (Used vs Total) and Mem row. When both are high, memory pressure is severe.
  • Run cat /proc/sys/vm/swappiness to see vm.swappiness. Default 60.
  • Good: You know usage and swappiness. Proceed to Add swap or tune swappiness.
  • Bad: Run swapon --show to confirm swap devices.

Add swap or tune swappiness

Goal: Reduce swap thrashing or add swap capacity.

  • When swappiness is high and the system is slow: sudo sysctl vm.swappiness=10. Add vm.swappiness=10 to /etc/sysctl.conf to persist.
  • When swap is too small: create a swap file. 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.
  • Good: Swappiness lowered or swap added. Check free -h to confirm.
  • Bad: Memory pressure persists—check for leaks. See When to escalate.

Find memory-heavy processes

Goal: Identify processes that consume memory.

  • Run ps aux --sort=-%mem | head -20 or top. Restart leaky services: sudo systemctl restart <service>.
  • For long-term: set MemoryMax in the systemd unit to cap memory.
  • Good: Memory usage drops after restart. Monitor with free -h.
  • Bad: OOM kills continue—see fix-linux-server-runs-out-of-memory or escalate.

When to escalate

Escalate if:

  • OOM kills continue after adding swap and tuning.
  • You cannot add swap (e.g. no disk space).
  • You need to add RAM.

Provide free -h, swapon --show, and vm.swappiness before escalating.

Verification

  • free -h shows swap headroom or reduced pressure.
  • swapon --show lists active swap devices.
  • No OOM killer messages in dmesg (or fewer after changes).
  • Applications run without being killed.

Escalation ladder

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

  1. Check swap and memory Run free -h and swapon --show.
  2. Tune swappiness Lower vm.swappiness to reduce swap thrashing.
  3. Add swap file fallocate, mkswap, swapon; add to fstab.
  4. Find memory-heavy processes ps aux --sort=-%mem; restart leaky services.
  5. Escalate Provide free -h, swapon 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
  • swapon --show output
  • vm.swappiness value
  • Steps already tried

Is swap full and memory high?

Run free -h to check Swap and Mem.

Run `free -h`. Swap full, Mem high: memory pressure. Swap full, Mem low: swappiness may be too high. Swap low: add swap. Both OK: no action needed.

You can change your answer later.

Is swappiness too high?

Check cat /proc/sys/vm/swappiness. Default 60.

Run `cat /proc/sys/vm/swappiness`. High (e.g. 60+): lower to 10 with `sysctl vm.swappiness=10`. Low: swap may be too small—add swap file.

You can change your answer later.

Lower swappiness

Run `sudo sysctl vm.swappiness=10`. Add to /etc/sysctl.conf to persist. Check `free -h` after a few minutes.

Is swap too small?

Run swapon --show to see total swap.

Run `swapon --show`. Small or none: add swap file with fallocate, mkswap, swapon. Adequate: check for memory leaks—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.

Check for memory leaks

Run `ps aux --sort=-%mem | head -20`. Restart leaky services. If OOM kills continue, see fix-linux-server-runs-out-of-memory or escalate.

Reviewed by Blackbox Atlas

Frequently asked questions

Why does swap fill up?
The kernel uses swap when RAM is low. High swap usage means memory pressure—either too little RAM, a memory leak, or vm.swappiness set too high.
Can I add more swap?
Yes. Create a swap file with fallocate, mkswap, swapon. Or add a swap partition. Swap on disk is slower than RAM but can prevent OOM kills.
When should I escalate a full swap?
If you cannot add swap, applications are OOM-killed, or you need to add RAM. Provide free -h, swapon, and vm.swappiness before escalating.

Rate this guide

Was this helpful?

Thanks for your feedback.

Continue to