Swap Management
Swap is disk space used as an overflow when physical RAM is full. The kernel moves inactive memory pages to swap, freeing RAM for active processes. On modern servers with plenty of RAM, swap is rarely used for overflow, but it still matters: swap space prevents OOM (Out-of-Memory) killer from terminating processes during unexpected memory spikes, and it is required for hibernation.
What swap does and when it matters
Physical RAM: [Process A][Process B][Process C][Free]
|
All RAM full
|
Kernel moves INACTIVE pages to swap
|
Physical RAM: [Process A][Process B][Free][Free]
Swap disk: [Process C pages]
If Process C needs its pages again → swap IN (slow, causes latency spike)| RAM size | Recommended swap | Reason |
|---|---|---|
| <2 GB | 2x RAM | Low RAM systems need swap as critical buffer |
| 2–8 GB | 1x RAM or at least 2 GB | Allows OOM protection and hibernation |
| 8–64 GB | 4–8 GB | Enough for OOM protection, not needed for hibernation |
| >64 GB | 4–8 GB | Mainly for OOM protection buffer |
Checking swap usage
# Quick overview: RAM and swap usage
free -h
free -h output
total used free shared buff/cache available
Mem: 15Gi 4.2Gi 8.1Gi 220Mi 2.7Gi 10Gi
Swap: 2.0Gi 0B 2.0Gi
# Detailed swap information
swapon --show
# Monitor which processes are using swap
for pid in /proc/[0-9]*; do
proc_name=$(cat "$pid/comm" 2>/dev/null)
swap=$(awk '/VmSwap/{print $2}' "$pid/status" 2>/dev/null)
[ "${swap:-0}" -gt 0 ] && echo "$swap kB $proc_name"
done | sort -rn | head -10
# Real-time memory and swap monitoring
vmstat 5 # Update every 5 seconds; si/so = swap in/out
Creating a swap file
A swap file is easier to manage than a swap partition — you can create, resize, or delete it without repartitioning.
# Create a 4 GB swap file
sudo fallocate -l 4G /swapfile
# Set correct permissions (world-readable swap is a security risk)
sudo chmod 600 /swapfile
# Format as swap
sudo mkswap /swapfile
# Enable swap
sudo swapon /swapfile
# Verify it's active
swapon --show
free -h
# Make it permanent (add to /etc/fstab)
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
📝 NOTE: Swap files on Btrfs file systems require special handling — you must create them with
ddinstead offallocate, and the Btrfs copy-on-write attribute must be disabled (chattr +C /swapfilebefore writing). On ext4 and XFS,fallocateworks fine.
Tuning swappiness
vm.swappiness controls how aggressively the kernel moves memory to swap. Values range from 0 to 200 (kernel 3.5+). The default is 60.
| Value | Behavior | Use for |
|---|---|---|
| 0 | Avoid swapping unless absolutely necessary (OOM risk) | Latency-critical databases |
| 1 | Near-minimum swapping | Database servers (MySQL, PostgreSQL recommend 1) |
| 10 | Prefer using file cache over swapping | Web servers, application servers |
| 60 | Default Ubuntu behavior | General purpose desktops |
| 100 | Swap aggressively | Not recommended for servers |
# Check current swappiness
cat /proc/sys/vm/swappiness
# Set temporarily (takes effect immediately)
sudo sysctl vm.swappiness=10
# Set permanently
echo "vm.swappiness=10" | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl -p /etc/sysctl.d/99-swappiness.conf
Removing or disabling swap
# Disable swap temporarily
sudo swapoff /swapfile
# Disable swap entirely (not recommended on servers — leaves you vulnerable to OOM)
sudo swapoff -a # Disable all swap
# Remove the swap file permanently
sudo swapoff /swapfile
sudo rm /swapfile
# Remove the /etc/fstab entry
sudo nano /etc/fstab # Delete the /swapfile line
Conclusion
Every Ubuntu server should have some swap space — at least 2–4 GB — even with plenty of RAM. It prevents the OOM killer from terminating processes during unexpected memory spikes. Create it as a swap file (fallocate -l 4G /swapfile) rather than a partition for flexibility. Set vm.swappiness=10 for application servers and vm.swappiness=1 for database servers so the kernel prioritizes keeping data in RAM. Add swap to /etc/fstab so it persists across reboots.
FAQ
Is Swap Management important for Ubuntu administrators?+
Yes. It supports practical Ubuntu administration because it connects directly to server reliability, security, troubleshooting, or daily operations.
Should I practice this on a live server?+
Use a lab VM first. After you understand the command output and rollback path, apply the workflow carefully on real systems.
What should I do after reading this article?+
Run the practice commands, write down what each one shows, and continue to the next article in the Ubuntu roadmap.
Need help with Ubuntu administration?
Work directly with Muhammad Irfan Aslam for Ubuntu Server, Linux, cloud, Docker, DevOps, CI/CD, or infrastructure troubleshooting support.
Hire Me for Support