Swap Management - Avoiding Memory Exhaustion in Linux

Swap Management - Avoiding Memory Exhaustion in Linux

What is Swap?

Swap is a virtual memory area on disk that Linux uses when physical RAM is full. It acts as a safety net — when RAM runs out, processes can continue running instead of being immediately killed. However, because swap involves disk I/O, it is orders of magnitude slower than RAM.

There are two forms of swap:

Form Description
Swap partition Dedicated disk partition. Slightly faster but hard to resize
Swapfile Regular file on the filesystem. Easy to resize, now the standard approach

Swap does not solve memory shortages — it delays them. Sustained swapping (thrashing) is a performance problem and a sign of an underlying memory issue. Adding more swap buys time but is not a fix.

How to Check Swap Usage

free -h is the fastest way to check swap. Watch the Swap: row — if used grows over time, the system is swapping.

free -h
               total        used        free      shared  buff/cache   available
Mem:           3.8Gi       2.1Gi       408Mi        45Mi       1.3Gi       1.4Gi
Swap:          2.0Gi       512Mi       1.5Gi

To list swap devices with detail, use swapon --show:

swapon --show
NAME      TYPE SIZE USED PRIO
/swapfile file   2G 512M   -2

To observe swap activity over time, use vmstat. Watch the si (swap in) and so (swap out) columns:

vmstat 1 5
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0 524288 418032  98304 1350656    0    0     2     5   45   80  2  1 97  0  0
 0  1 524288 200000  98304 1350672    8   12     0    20   65  120  2  1 90  7  0

Sustained non-zero si/so values indicate thrashing.

How to Create a Swapfile

The standard procedure on Ubuntu / Debian systems to add swap capacity:

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

If fallocate is not available (NFS or some filesystems), use dd instead: dd if=/dev/zero of=/swapfile bs=1M count=2048

Verify with swapon --show:

swapon --show
NAME      TYPE SIZE USED PRIO
/swapfile file   2G   0B   -2

Make Swap Persistent Across Reboots

Add an entry to /etc/fstab:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

After editing /etc/fstab, validate with sudo swapon -a before rebooting. A syntax error in fstab can prevent the system from booting.

Remove a Swapfile

sudo swapoff /swapfile
sudo rm /swapfile

Also remove the corresponding line from /etc/fstab.

How to Tune Swappiness

swappiness is a kernel parameter (0–100) that controls how aggressively the kernel uses swap. The default is 60.

Value Behavior
0 Only swap when RAM is nearly exhausted (does not disable swap entirely)
10–30 Desktop/workstation workloads — prefer RAM
60 Default
100 Swap aggressively

Check the current value:

cat /proc/sys/vm/swappiness

Change temporarily (resets on reboot):

sudo sysctl vm.swappiness=10

Persist the setting:

echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl -p /etc/sysctl.d/99-swappiness.conf

For database servers (MySQL, PostgreSQL), lowering swappiness to 1–10 is a common production tuning step. Swapping under database workloads causes query latency spikes that are difficult to diagnose without this context.

What Happens When Memory is Exhausted?

When both RAM and swap are full, the kernel's OOM killer (Out-Of-Memory killer) terminates a process to free memory. This causes unexpected application crashes. Check for OOM kills in the kernel log:

dmesg | grep -i 'oom\|killed process\|out of memory'
[123456.789] Out of memory: Killed process 1234 (mysqld) total-vm:2097152kB, anon-rss:1048576kB

Or via the systemd journal:

journalctl -k | grep -i 'oom\|killed'

Protect a Specific Process from OOM Kill (Temporary)

Adjust the OOM score for a critical process. Values range from -1000 (never kill) to 1000 (kill first):

echo -1000 | sudo tee /proc/$(pgrep mysqld)/oom_score_adj

This setting does not persist across restarts. Use it only as a short-term workaround while addressing the underlying memory issue.

Next Reading