Adjust Swappiness Parameter
The swappiness
parameter controls how aggressively the kernel moves memory pages to swap. A lower value (e.g., 10–30) reduces reliance on swap, keeping more data in physical RAM for faster access—ideal for systems with sufficient memory. For memory-intensive workloads like databases, set it even lower (10–15) to minimize disk I/O latency. Temporarily adjust with sudo sysctl vm.swappiness=10
, or permanently add vm.swappiness=10
to /etc/sysctl.conf
and apply changes with sudo sysctl -p
. This is a foundational step for balancing performance and stability.
Optimize Swap Device Performance
Using a fast storage device for swap significantly improves performance. SSDs (especially NVMe) offer much higher read/write speeds than traditional HDDs, reducing swap latency. If your system has an SSD, prioritize using it for swap over HDDs. Avoid placing swap on network-attached storage (NAS) unless absolutely necessary, as network latency can further degrade performance.
Properly Size Swap Space
Swap size should align with your system’s memory and workload. General guidelines:
free -h
or swapon --show
to avoid over- or under-provisioning.Use Swap Files Instead of Partitions
Swap files are more flexible than partitions: they can be easily resized, moved, or removed without repartitioning the disk. To create a 4GB swap file:
sudo fallocate -l 4G /swapfile # Faster than dd (if supported)
sudo chmod 600 /swapfile # Restrict permissions for security
sudo mkswap /swapfile # Format as swap space
sudo swapon /swapfile # Enable immediately
Add /swapfile none swap sw 0 0
to /etc/fstab
to enable it at boot. Swap files are ideal for cloud instances or systems without dedicated swap partitions.
Monitor and Analyze Swap Usage
Regular monitoring helps identify performance bottlenecks. Use these tools:
free -h
: View total, used, and free swap/physical memory.swapon --show
: Display detailed swap device info (size, usage, priority).top
/htop
: Check processes consuming the most memory (sort by “RES” or “MEM%”).vmstat 1
: Monitor swap activity (si
= swap in, so
= swap out)—frequent swaps indicate high usage./var/log/syslog
) for swap-related warnings (e.g., “Out of memory” errors). Proactive monitoring prevents sudden performance degradation.Adjust Kernel Parameters for Swap Behavior
Beyond swappiness
, tweak other kernel parameters to optimize swap usage:
vm.vfs_cache_pressure
: Controls how aggressively the kernel reclaims memory used for caching directory and inode objects. Lower values (e.g., 50) retain more cache, reducing unnecessary disk I/O. Add vm.vfs_cache_pressure=50
to /etc/sysctl.conf
and run sudo sysctl -p
to apply.vm.dirty_ratio
/vm.dirty_background_ratio
: Adjust how frequently dirty pages (modified but unsaved memory) are written to disk. Lower values (e.g., vm.dirty_ratio=20
, vm.dirty_background_ratio=10
) reduce write pressure on swap, but may increase memory usage temporarily.Reduce Unnecessary Swap Usage
Minimize applications that consume excessive memory or rely heavily on swap. For example:
sudo systemctl disable <service>
) to free up RAM.ufw
instead of iptables
for firewalls) to reduce background memory usage.--memory
and --memory-swap
flags) to prevent them from exhausting system memory and triggering excessive swapping.Advanced: Use zram for Compressed RAM Swap
For systems with limited disk space or slow disks, zram creates a compressed block device in RAM, acting as swap. This reduces disk I/O and improves performance for memory-heavy workloads. Install zram tools:
sudo apt update && sudo apt install zram-tools
Configure zram (e.g., allocate 2GB compressed RAM for swap) by editing /etc/default/zramswap
and enabling it:
sudo systemctl enable --now zramswap.service
zram is particularly useful for laptops or low-end servers where disk performance is a bottleneck.