Swap in use is not a problem by itself; active swapping is. Run vmstat 1 and look at the si and so columns: if they stay non-zero, the server is moving memory to and from disk and is short of RAM. Find the processes using the most memory and reduce them, or add RAM.
Symptoms
- free -m shows swap used growing
- Server becomes very slow under load
- High iowait with the swap device busy
How to diagnose it
Check swap and available memory
Low available memory and high swap together mean pressure.
free -m swapon --showCheck whether swapping is happening now
si is memory swapped in, so swapped out, per second. Sustained non-zero values mean active swapping.
vmstat 1 10Find which processes are swapped out
VmSwap in each process’s status file shows its swapped memory.
for f in /proc/[0-9]*/status; do awk '/^Name|^VmSwap/{printf "%s ", $2} END{print ""}' "$f"; done 2>/dev/null | sort -k2 -rn | headCheck swappiness
Default is usually 60. Lower values make the kernel prefer dropping cache over swapping applications.
cat /proc/sys/vm/swappiness
Common causes and fixes
Not enough RAM for the workload
Reduce memory use (fewer workers, smaller caches) or resize the server.
Old, idle pages swapped out long ago
If si/so are zero, swap use is harmless. To pull pages back into RAM when memory is plentiful, cycle swap.
sudo swapoff -a && sudo swapon -a # only when free RAM exceeds used swapSwappiness too high for a database server
Lower it so the kernel keeps application memory resident.
echo "vm.swappiness=10" | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --systemMemory leak
A process whose RSS keeps growing pushes everything else into swap. Restart it and fix the leak.
Prevent it happening again
- Alert when SWAP_used is above 50% for 10 minutes.
- Alert on MEM_used above 90% so you see pressure before swapping starts.
- Watch CPU_iowait; swapping shows up as disk wait.
Set it up: Linux monitoring · Ubuntu monitoring.
FAQ
Is it bad if Linux is using swap?
Not necessarily. The kernel moves idle pages to swap to make room for cache. It is only a problem when pages are swapped in and out continuously.
How do I clear swap in Linux?
Run sudo swapoff -a && sudo swapon -a, but only when available RAM is larger than used swap, otherwise the OOM killer may run.
How much swap should a server have?
For most servers 1–4 GB or about half of RAM on small machines is enough as a safety margin. Swap is not a substitute for RAM.
Related guides
Get alerted before this becomes an outage.
Install the agent in one line. Statance watches CPU, memory, disk and services, and its AI assistant helps find the cause.