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 sizeRecommended swapReason
<2 GB2x RAMLow RAM systems need swap as critical buffer
2–8 GB1x RAM or at least 2 GBAllows OOM protection and hibernation
8–64 GB4–8 GBEnough for OOM protection, not needed for hibernation
>64 GB4–8 GBMainly 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 dd instead of fallocate, and the Btrfs copy-on-write attribute must be disabled (chattr +C /swapfile before writing). On ext4 and XFS, fallocate works 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.

ValueBehaviorUse for
0Avoid swapping unless absolutely necessary (OOM risk)Latency-critical databases
1Near-minimum swappingDatabase servers (MySQL, PostgreSQL recommend 1)
10Prefer using file cache over swappingWeb servers, application servers
60Default Ubuntu behaviorGeneral purpose desktops
100Swap aggressivelyNot 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