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.
What you'll need
- SSH or console access with sudo
Step-by-step diagnostic
Quick triage — pick your path
Get started
Choose the option that matches what you see. You can jump straight to that section.
Show full guide
Steps
Goal: Confirm swap usage, tune swappiness, add swap if needed, and address memory pressure.
- Run
free -hto confirm swap and memory usage. Runswapon --showto 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/swappinessto see vm.swappiness. Default 60. - Good: You know usage and swappiness. Proceed to Add swap or tune swappiness.
- Bad: Run
swapon --showto 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. Addvm.swappiness=10to /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 0to /etc/fstab. - Good: Swappiness lowered or swap added. Check
free -hto 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 -20ortop. Restart leaky services:sudo systemctl restart <service>. - For long-term: set
MemoryMaxin 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 -hshows swap headroom or reduced pressure.swapon --showlists 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.
- Check swap and memory Run free -h and swapon --show.
- Tune swappiness Lower vm.swappiness to reduce swap thrashing.
- Add swap file fallocate, mkswap, swapon; add to fstab.
- Find memory-heavy processes ps aux --sort=-%mem; restart leaky services.
- 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.
You can change your answer later.
Is swappiness too high?
Check cat /proc/sys/vm/swappiness. Default 60.
You can change your answer later.
Lower swappiness
Is swap too small?
Run swapon --show to see total swap.
You can change your answer later.
Add swap file
Check for memory leaks
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.