Why Swap Space Matters

Even with ample RAM, temporary memory spikes can crash your VPS. Swap space provides emergency memory on disk, preventing out-of-memory (OOM) errors. This guide explains how to add, size, and optimize swap for different workloads.

What is Swap and How It Works

Swap is disk space used as overflow for RAM. When physical memory fills, the kernel moves idle pages to swap, freeing RAM for active processes. Swap is slower than RAM (disk vs memory), but better than crashing. For VPS, swap prevents OOM killer from terminating critical processes (MySQL, web servers). Modern systems use swap proactively (swappiness) for rarely accessed data.

Determining Swap Size

Traditional rule: twice RAM (obsolete). Modern recommendations: For RAM <2GB: swap = 2x RAM. 2-8GB RAM: swap = same as RAM. 8-64GB RAM: swap = 0.5x RAM. 64GB+ RAM: swap = 4-8GB (enough for crash dumps). For VPS with 4GB RAM, recommended 2-4GB swap. Database servers: more swap (helps with large sorts). Web servers: less swap (swap rarely needed). Monitor: if swap usage >10% consistently, add RAM.

Step 1: Check Current Swap

Check existing swap: swapon --show or free -h. If no output, swap disabled. Check disk space: df -h (ensure free space for swap file). Recommendation: use swap file (more flexible than partition). For Hostxpeed VPS, default no swap (but can add).

Step 2: Create Swap File

Create file (example 2GB): sudo fallocate -l 2G /swapfile (fallocate fastest) OR sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 (fallback). Set permissions: sudo chmod 600 /swapfile (only root can read). Format as swap: sudo mkswap /swapfile. Enable: sudo swapon /swapfile. Verify: swapon --show. Make permanent: add to /etc/fstab: echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab.

Step 3: Configure Swappiness

Swappiness (0-100): tendency to use swap. Default 60 (aggressive). For VPS, recommended 10-30 (use swap only when necessary). Check current: cat /proc/sys/vm/swappiness. Set temporary: sudo sysctl vm.swappiness=20. Permanent: edit /etc/sysctl.conf, add vm.swappiness=20. For database servers: swappiness=1 (avoid swap). For desktop-like VPS: swappiness=60.

Step 4: Tuning for Performance

vfs_cache_pressure (0-1000): tendency to reclaim dentry/inode cache. Default 100. For swap-heavy workloads, reduce to 50 (retain cache). Add to sysctl.conf: vm.vfs_cache_pressure=50. dirty_ratio/dirty_background_ratio: control write caching. For swap on HDD: lower values (avoid swap thrash). For SSD/NVMe (Hostxpeed): defaults fine.

Step 5: Monitoring Swap Usage

Real-time: free -h (si/so columns indicate swap in/out). vmstat 1 (si/so columns). sar -S (if sysstat installed). High si/so: system thrashing (need more RAM). Use htop (sort by RES, swap column). Alert: monitor swap usage (if >20%, investigate). Hostxpeed Control Panel metrics includes swap usage graph.

Step 6: Removing or Resizing Swap

Disable swap: sudo swapoff /swapfile. Delete file: sudo rm /swapfile. Resize: disable, delete, recreate with new size. Changing swap size doesn't require reboot. For permanent removal, delete /etc/fstab entry. Caution: disable swap only if sufficient RAM (free -h shows available).

Use Case: MySQL Server

MySQL uses swap for large queries, sorts, temporary tables. Without swap, MySQL may crash with 'out of memory'. With 8GB RAM, add 4GB swap. Swappiness=10. Monitor InnoDB buffer pool hit ratio (>99%). If swapping occurs, increase innodb_buffer_pool_size? Actually, reduce if swapping.

Use Case: PHP-FPM + Nginx

PHP processes memory spikes common (unserializing large data). Swap provides cushion. Swappiness=20. pm.max_requests lower to limit memory growth. Monitor swap usage; if constant, increase max_children or add RAM.

Use Case: Node.js Applications

Node.js memory leaks cause crash without swap. With swap, process may become slow but remain running. Swappiness=10. Use --max-old-space-size flag (limit heap). Swap as safety net, not solution. Also use PM2 restart on memory threshold.

Conclusion: Essential Safety Net

Swap space prevents VPS crashes during temporary memory spikes. Add 2-4GB swap for typical VPS (2-8GB RAM). Adjust swappiness to 10-30. Monitor with free -h. For production, ensure enough RAM; swap is emergency backup, not performance solution.