Swap File Configuration & Linux Memory Management
Create and configure a Swap file on VirtFusion KVM instances, adjust vm.swappiness, and prevent Out of Memory (OOM) crashes under spike workloads.
When running memory-intensive workloads like Docker containers, MySQL databases, or compilation scripts, physical RAM can spike and exhaust rapidly. Under low memory pressure, the Linux kernel invokes the OOM Killer (Out-of-Memory Killer), abruptly killing essential services. Because FlawlessNode cloud servers are built on native VirtFusion KVM virtualization, you have full kernel control to allocate virtual swap space as a safety buffer.
1. Checking Memory and Swap Status
Before creating a new swap file, verify whether any swap partition or file is currently active:
# Display physical RAM and swap capacity
free -h
# Check active swap devices
swapon --show
If swapon --show returns no output and the Swap row in free -h displays 0B, no swap space is currently configured.
2. Creating and Enabling a Swap File
For instances with 1GB to 4GB of RAM, allocating a 1GB~2GB swap file is standard. For instances with 8GB or more, a 2GB~4GB swap file is generally sufficient.
2.1 Pre-allocating the Swap File
Use fallocate to pre-allocate a 2GB file instantly (or fall back to dd if required by the filesystem):
# Recommended: Fast 2GB allocation
sudo fallocate -l 2G /swapfile
# Fallback (if fallocate is unsupported):
# sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
2.2 Securing File Permissions
Swap files contain memory page copies and must not be readable by unprivileged users:
sudo chmod 600 /swapfile
2.3 Formatting and Activating Swap
Format the file as Linux swap space and activate it:
# Format as swap
sudo mkswap /swapfile
# Example output: Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
# Enable swap
sudo swapon /swapfile
Verify with free -h:
free -h
# The Swap row now reflects 2.0GiB of available capacity
3. Persisting Swap Mount Across Reboots
To ensure the swap file mounts automatically after system reboots, add it to /etc/fstab:
# Backup existing fstab
sudo cp /etc/fstab /etc/fstab.bak
# Append swapfile mount entry
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
[!NOTE] Unlike restricted container environments (such as OpenVZ or LXC), FlawlessNode VirtFusion KVM instances grant complete kernel privileges, allowing you to manage swap spaces and virtual memory policies freely.
4. Fine-Tuning vm.swappiness
The vm.swappiness sysctl parameter controls how aggressively the Linux kernel moves memory pages into swap (0~100):
- Default (60): Tuned for desktop or rotational disk systems, which can lead to premature swap usage on high-performance cloud servers;
- Recommended (10~20): Instructs the kernel to prioritize fast physical RAM and only page out to NVMe swap when free memory drops to a critical threshold.
Adjusting swappiness and Cache Pressure
# Check current swappiness (typically 60)
cat /proc/sys/vm/swappiness
# Temporarily set to 10
sudo sysctl vm.swappiness=10
Persist this setting in /etc/sysctl.d/99-swap-tuning.conf:
cat << 'EOF' | sudo tee /etc/sysctl.d/99-swap-tuning.conf
# Prioritize physical RAM to maintain low latency
vm.swappiness = 10
# Tune directory and inode cache retention (default 100, recommended 50)
vm.vfs_cache_pressure = 50
EOF
# Reload sysctl
sudo sysctl --system
[!TIP] If your server primarily hosts database engines like MySQL / MariaDB / PostgreSQL, setting
vm.swappinessto1or10prevents critical buffer pools from being paged to disk, avoiding sudden latency spikes.
5. Resizing or Removing a Swap File
If you upgrade your server specs and no longer need swap, remove it safely:
# 1. Deactivate the swap file
sudo swapoff /swapfile
# 2. Remove the entry from /etc/fstab
sudo sed -i '\|/swapfile none swap sw 0 0|d' /etc/fstab
# 3. Delete the file from disk
sudo rm -f /swapfile