Swap Configuration
Linux systems use swap space as a fallback when physical memory (RAM) is exhausted. While modern systems with ample RAM may minimize reliance on swap, it remains critical for stability in low-memory environments. Proper swap configuration ensures predictable performance under memory pressure, especially for systems with limited RAM (e.g., embedded devices, older hardware, or lightweight servers).
Creating Swap Space¶
Swap space can be implemented as a dedicated partition or a file. Both methods are valid, but swap files offer greater flexibility for dynamic environments.
1. Create a Swap File¶
# Create a 4GB swap file (adjust size as needed)
sudo fallocate -l 4G /swapfile
# Set appropriate permissions
sudo chmod 600 /swapfile
# Format as swap
sudo mkswap /swapfile
# Enable swap
sudo swapon /swapfile
# Verify
sudo swapon --show
2. Create a Swap Partition¶
Use fdisk or parted to create a partition, then format and enable it:
# Example with fdisk (replace /dev/sdX with your disk)
sudo fdisk /dev/sdX
# Format the partition
sudo mkswap /dev/sdX1
# Enable swap
sudo swapon /dev/sdX1
# Add to /etc/fstab for persistence
echo '/dev/sdX1 none swap sw 0 0' | sudo tee -a /etc/fstab
Sizing Swap Space¶
Traditional guidelines suggest 1–2× RAM for swap size, but this depends on workload: - Low-RAM systems (<4GB): Aim for 2× RAM to avoid OOM killers. - High-memory workloads (e.g., databases): Use 1× RAM or adjust based on observed memory usage. - Modern systems (>16GB): Swap may rarely be used, but keep at least 1× RAM for safety.
Avoid over-provisioning: Excessive swap can cause thrashing (constant swapping between RAM and disk), degrading performance. Monitor with:
Configuring Swap Behavior¶
1. Adjust swappiness¶
The swappiness parameter (0–100) controls how aggressively the kernel swaps:
- 0: Prioritize using RAM; swap only when absolutely necessary.
- 100: Aggressively swap to free RAM.
To persist the setting:
# Temporarily adjust (restarts only)
sudo sysctl vm.swappiness=10
# Permanently set (edit /etc/sysctl.conf)
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
sudo sysctl --system
2. Tune vfs_cache_pressure¶
This parameter influences how aggressively the kernel evicts inode caches:
# Example: Increase pressure to prioritize application memory
echo 'vfs_cache_pressure=500' | sudo tee -a /etc/sysctl.conf
sudo sysctl --system
Troubleshooting Swap Issues¶
- High swap usage: Use
toporhtopto identify memory-hungry processes. Optimize or kill non-critical apps. - Swap not available: Check
/etc/fstabfor syntax errors or missing entries. Useswapon --showto verify active swap. - Kernel warnings: Monitor
/var/log/dmesgfor messages likeSWAP: Not enough roomorOOM killeralerts.
Key takeaways¶
- Create swap via files (flexible) or partitions (persistent) based on system needs.
- Size swap proportionally to RAM and workload, avoiding over-provisioning.
- Tune
swappinessto balance memory usage and performance, with lower values for latency-sensitive systems. - Monitor swap activity using
free,vmstat, and process tools to identify bottlenecks.