Linux Swap Space Management: Configuration and Optimization
How to configure and optimize swap space on Linux, including swap files, swap partitions, and tuning swappiness for different workloads.
Introduction
Swap space is the overflow area for physical RAM on Linux. When memory is full, the kernel moves inactive pages to swap to free up RAM. While swap prevents immediate OOM kills, excessive swapping degrades performance. Properly configuring swap space and tuning swappiness is essential for server performance. This post covers swap management on Linux.
Environment
The examples use Ubuntu 22.04 LTS with 16 GB of RAM, running a mix of web servers and databases that have different swap requirements.
free -h
# total used free shared buff/cache available
# Mem: 15Gi 8Gi 2Gi 512Mi 5Gi 6Gi
# Swap: 2.0Gi 0B 2.0Gi
swapon --show
# NAME TYPE SIZE USED PRIO
# /dev/sda2 partition 2G 0B -2Problem
The server is swapping heavily under load:
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
# 5 0 524288 262144 262144 4194304 1024 2048 0 0 0 0 45 5 50 0The si (swap in) and so (swap out) values indicate active swapping, which degrades performance.
Analysis
Swap behavior is controlled by vm.swappiness:
| Value | Behavior |
|---|---|
| 0 | Avoid swapping as much as possible |
| 1-10 | Low swappiness, prefer RAM |
| 10-60 | Balanced |
| 60-100 | Aggressively swap |
The default value (60 on most distributions) is a reasonable starting point but may not be optimal for all workloads.
Check swap usage:
# Check total swap usage
swapon --show
# NAME TYPE SIZE USED PRIO
# /dev/sda2 partition 2G 512M -2
# Check per-process swap usage
for pid in $(ls /proc/ | grep -E '^[0-9]+$'); do
swap=$(awk '/VmSwap/{print $2}' /proc/$pid/status 2>/dev/null)
if [ "$swap" -gt 0 ] 2>/dev/null; then
name=$(cat /proc/$pid/comm 2>/dev/null)
echo "$pid $name ${swap}kB"
fi
done | sort -k3 -n -r | head -10Solution
1. Create a swap file
# Create a 4GB swap file
sudo fallocate -l 4G /swapfile
# Or use dd for older systems
# sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
# Set permissions
sudo chmod 600 /swapfile
# Format as swap
sudo mkswap /swapfile
# Setting up swapspace version 1, size = 4 GiB (4294967296 bytes)
# Enable
sudo swapon /swapfile
# Verify
swapon --show
# NAME TYPE SIZE USED PRIO
# /swapfile file 4G 0B -22. Make swap persistent
# Add to /etc/fstab
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab3. Set swappiness
# For database servers (minimize swapping)
sudo sysctl vm.swappiness=10
# For application servers (moderate swapping)
sudo sysctl vm.swappiness=30
# For desktop systems (allow more swapping)
sudo sysctl vm.swappiness=60
# Persist the change
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.d/99-swap.conf4. Create multiple swap files
# Create 4 x 2GB swap files
for i in $(seq 1 4); do
sudo fallocate -l 2G /swapfile${i}
sudo chmod 600 /swapfile${i}
sudo mkswap /swapfile${i}
sudo swapon /swapfile${i}
done
# Set priority for even distribution
sudo swapoff /swapfile1
sudo mkswap -p 10 /swapfile1
sudo swapon /swapfile15. Resize swap
# Disable swap
sudo swapoff /swapfile
# Resize file
sudo fallocate -l 8G /swapfile
# Or truncate and recreate
# sudo truncate -s 8G /swapfile
# Reformat
sudo mkswap /swapfile
# Re-enable
sudo swapon /swapfile6. Monitor swap usage
# Check swap usage
free -h
# total used free shared buff/cache available
# Swap: 4.0Gi 512.0Mi 3.5Gi
# Real-time swap monitoring
vmstat 1
# Check swap in/ou t from /proc
cat /proc/vmstat | grep -E "pswpin|pswpout"
# pswpin 1234567
# pswpout 23456787. Use zram for compressed swap
# Install zram-tools
sudo apt-get install zram-tools
# Configure
sudo nano /etc/default/zramswap
# PERCENTAGE=50
# PRIORITY=100
# Start
sudo systemctl enable zramswap
sudo systemctl start zramswap
# Verify
zramctl
# NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
# /dev/zram0 lz4 8.0G 512M 200M 250M 4 [SWAP]8. Configure vm.min_free_kbytes
# Reserve memory for kernel
# Set to about 1-5% of physical RAM
sudo sysctl vm.min_free_kbytes=131072
# Persist
echo "vm.min_free_kbytes=131072" | sudo tee -a /etc/sysctl.d/99-swap.conf9. Use swap for specific workloads
# Disable swap for a specific process
# Use cgroups
sudo mkdir /sys/fs/cgroup/no-swap
echo 0 | sudo tee /sys/fs/cgroup/no-swap/memory.swap.max
echo $PID | sudo tee /sys/fs/cgroup/no-swap/cgroup.procs10. Check swap performance
# Benchmark swap speed
dd if=/dev/urandom of=/swapfile bs=1M count=1024 oflag=direct
sync
# Test swap access time
swapon --show --bytesLessons Learned
Swap space is not a substitute for adequate RAM, but it is essential for handling memory spikes. For database servers, set vm.swappiness low (10 or less) to minimize swapping. For general servers, 30 is a good balance. Consider zram for compressed swap on systems with limited disk space. And always monitor swap usage to identify processes that may need memory tuning.
This blog does not accept any external sponsorships, affiliate marketing, or ad revenue.