VPS Swap Space Configuration: When and How to Use Swap for Better Performance

Swap space is a critical safety net for Linux VPS servers. When physical RAM fills up, the kernel moves inactive memory pages to disk-based swap, preventing the Out-Of-Memory (OOM) killer from abruptly terminating your applications. However, swap is not a substitute for adequate RAM — it is a fallback mechanism that trades performance for stability.

This guide covers when to use swap, the swap file vs. partition debate, how to tune swappiness for VPS workloads, and how to monitor swap effectively. If you are on a low-RAM VPS (1 GB or less), proper swap configuration can mean the difference between a stable server and constant OOM crashes. Compare VPS plans with sufficient RAM on our site.

Swap File vs. Swap Partition: Which Should You Use?

FeatureSwap FileSwap Partition
Resize flexibilityEasy to resize (delete + recreate)Requires repartitioning
PerformanceSame as partition on modern kernelsSlightly better on legacy kernels
Setup complexitySimple (create file, mkswap, swapon)Requires disk partitioning
Best forCloud VPS, changeable environmentsBare metal, fixed configurations

For virtually all cloud VPS setups, a swap file is the recommended choice. It requires no repartitioning, can be resized without reformatting, and performs identically to a swap partition on Linux kernel 5.x and later.

Swap Size Recommendations by VPS RAM

VPS RAMRecommended SwapTypical WorkloadsNotes
512 MB – 1 GB1 GB – 2 GBLight web servers, proxiesHeavy swap dependency expected
2 GB1 GB – 2 GBWordPress, small databasesSwap used during traffic spikes
4 GB1 GBMedium apps, multiple sitesSwap as safety net
8 GB+512 MB – 1 GBHeavy production workloadsMinimal swap; rely on RAM

Warning: If your VPS consistently uses more than 25% of swap under normal load, you need more RAM — not more swap.

Setting Up a Swap File on Linux VPS

# Allocate a 2 GB swap file\nsudo fallocate -l 2G /swapfile\n# Secure the file\nsudo chmod 600 /swapfile\n# Format as swap\nsudo mkswap /swapfile\n# Enable swap\nsudo swapon /swapfile\n# Make permanent\necho '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab\n# Verify\nsudo swapon --show\nfree -h

Tuning Swappiness for VPS Performance

SwappinessBehaviorBest For
0Only swap when absolutely necessaryHigh-risk OOM scenarios
10Recommended — avoids swap unless real pressureMost VPS workloads
60Default — conservativeDesktop systems
# Set swappiness to 10\nsudo sysctl vm.swappiness=10\necho 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf\nsudo sysctl -p

Monitoring Swap Usage

# Quick swap status\nfree -h\n# Detailed swap usage\nswapon --show

Conclusion

Swap space is an essential part of any Linux VPS configuration, but it must be sized and tuned correctly. Use a swap file for flexibility, set swappiness to 10, and monitor usage regularly. If you rely on swap daily, upgrade to a VPS plan with more RAM.

Leave a Reply