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?
| Feature | Swap File | Swap Partition |
|---|---|---|
| Resize flexibility | Easy to resize (delete + recreate) | Requires repartitioning |
| Performance | Same as partition on modern kernels | Slightly better on legacy kernels |
| Setup complexity | Simple (create file, mkswap, swapon) | Requires disk partitioning |
| Best for | Cloud VPS, changeable environments | Bare 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 RAM | Recommended Swap | Typical Workloads | Notes |
|---|---|---|---|
| 512 MB – 1 GB | 1 GB – 2 GB | Light web servers, proxies | Heavy swap dependency expected |
| 2 GB | 1 GB – 2 GB | WordPress, small databases | Swap used during traffic spikes |
| 4 GB | 1 GB | Medium apps, multiple sites | Swap as safety net |
| 8 GB+ | 512 MB – 1 GB | Heavy production workloads | Minimal 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 -hTuning Swappiness for VPS Performance
| Swappiness | Behavior | Best For |
|---|---|---|
| 0 | Only swap when absolutely necessary | High-risk OOM scenarios |
| 10 | Recommended — avoids swap unless real pressure | Most VPS workloads |
| 60 | Default — conservative | Desktop systems |
# Set swappiness to 10\nsudo sysctl vm.swappiness=10\necho 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf\nsudo sysctl -pMonitoring Swap Usage
# Quick swap status\nfree -h\n# Detailed swap usage\nswapon --showConclusion
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
You must be logged in to post a comment.