Swap space on a VPS is a double-edged sword. Used correctly, it acts as an emergency reservoir that prevents out-of-memory (OOM) kills when your server hits a memory spike. Used incorrectly, it slows your server to a crawl by treating your disk — even an NVMe SSD — as if it were RAM. This guide covers when swap helps, when it hurts, how to size it optimally, and how to tune the kernel’s swap behavior for your specific workload.
What Is Swap and How Does It Work?
Swap is disk space that the kernel uses as overflow when physical RAM is full. The kernel’s memory manager moves inactive memory pages from RAM to swap (a process called “paging out”) to free up physical memory for active processes. When those pages are needed again, they’re paged back in.
On a VPS, swap is typically configured as either:
- A swap partition — A dedicated disk partition formatted as swap.
- A swap file — A regular file on your filesystem used as swap (more flexible, resizable without repartitioning).
When Swap Helps vs When It Hurts
Swap Helps When:
- You have occasional memory spikes — A background cron job or traffic surge pushes memory usage above your RAM limit. Swap prevents the OOM killer from terminating your web server or database.
- You run idle processes — The kernel can swap out long-idle processes (e.g., a stale SSH session, an unused monitoring agent) to free RAM for active workloads.
- You’re provisioning a new server — Having some swap configured from day one gives you a safety net while you tune your application’s memory footprint.
- Hibernation is needed — Some VPS control panels use swap for suspend-to-disk functionality.
Swap Hurts When:
- Your workload is actively using swap — If
free -hshows significant swap usage (more than a few MB), your server is critically low on RAM. Swapping is 10-100x slower than RAM access, even on NVMe. Your application performance will degrade sharply. - You have HDD-based storage — On a VPS with HDD or even SATA SSD, heavy swapping causes severe I/O bottlenecks and can make SSH sessions unresponsive.
- Your database relies on direct memory access — PostgreSQL and MySQL perform best when their working set fits entirely in RAM. If they start swapping, query latency spikes by orders of magnitude.
Optimal Swap Size Based on RAM
The old rule of thumb (“swap = 2x RAM”) was designed for systems with small amounts of RAM and HDD storage. Modern VPS recommendations are different:
| VPS RAM | Recommended Swap | Notes |
|---|---|---|
| 512 MB – 1 GB | 1-2 GB | Low-RAM VPS benefits more from swap as a safety net |
| 2 GB | 1-2 GB | Useful for occasional spikes; consider 1 GB to start |
| 4 GB | 1 GB | Most workloads fit in RAM; swap is just insurance |
| 8 GB | 512 MB – 1 GB | Only needed for very rare OOM scenarios |
| 16 GB+ | 0-512 MB | Disable swap entirely or use a minimal file just for kernel crash dumps |
The key insight: swap size is not about having more “memory.” It’s about buying time during a spike. If your application consistently needs more memory than you have, add more RAM — don’t rely on swap.
Swap on SSD vs HDD: Performance Reality
Storage speed directly determines how painful swap usage is:
- NVMe SSD (3-7 GB/s) — Swap is tolerable for emergency use. A page fault costs ~10 microseconds instead of ~100 nanoseconds for RAM. You’ll notice slowdown but the server stays responsive.
- SATA SSD (500-600 MB/s) — Swapping is noticeably slow. Performance drops by 50-80% when actively swapping.
- HDD (80-160 MB/s) — Swapping on HDD is catastrophic. Random I/O on HDD for swap pages causes latencies of 5-10 milliseconds per access. Avoid at all costs.
When choosing a VPS, see performance specs on our VPS comparison page to find providers using NVMe storage, which makes swap a viable safety net rather than a performance killer.
How to Configure a Swap File on Linux
Creating a swap file is straightforward and doesn’t require repartitioning:
# Create a 1 GB swap file
sudo fallocate -l 1G /swapfile
# Set correct permissions (root only)
sudo chmod 600 /swapfile
# Format as swap
sudo mkswap /swapfile
# Enable it
sudo swapon /swapfile
# Make permanent (add to /etc/fstab)
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Verify it’s active:
sudo swapon --show
free -h
Tuning Swappiness for Your Workload
The vm.swappiness parameter (0-100) controls how aggressively the kernel swaps unused pages. The default is 60 — a conservative balance that works for general-purpose desktops. For server workloads, you’ll want to tune this:
| Swappiness Value | Behavior | Best For |
|---|---|---|
| 0 | Swap only when absolutely necessary (OOM imminent) | Databases (PostgreSQL, MySQL), Redis, in-memory caches |
| 1 | Minimal swapping — slight preference for keeping pages in RAM | Web servers, application servers, most production workloads |
| 10 | Conservative — swap idle processes but keep active ones in RAM | General-purpose VPS with mixed workloads |
| 60 (default) | Balance between RAM and swap usage | Desktop, development servers, low-memory systems |
| 100 | Aggressive swapping — kernel treats RAM and swap equally | Almost never recommended for servers |
Set swappiness temporarily:
sudo sysctl vm.swappiness=10
Make it permanent by adding to /etc/sysctl.conf:
vm.swappiness=10
Monitoring Swap Usage
Keep an eye on swap with these commands:
free -h— Quick overview of total, used, and free swap.swapon --show— Shows swap file/partition details and current usage.vmstat 1— Watch thesi(swap in) andso(swap out) columns. If these are consistently above zero, your server is actively swapping.htop— Each process shows its swapped memory percentage in theSWAPcolumn.
When to Add More RAM Instead of Swap
If your monitoring shows consistent swap usage (more than 100-200 MB actively swapped), don’t increase the swap file — upgrade your VPS’s RAM instead. Swap is an emergency brake, not a replacement for adequate memory. Adding more RAM eliminates the performance penalty entirely.
Most VPS providers make upgrading RAM as simple as changing your plan in the control panel. To compare plans with different RAM configurations and their pricing, check out our VPS comparison table to find a plan with the right memory-to-cost ratio for your workload.
Quick Reference
- Configure swap as insurance, not as primary memory.
- Use the recommended swap sizes based on your RAM.
- Set
vm.swappiness=1for database servers,10for general VPS workloads. - Monitor
siandsocolumns invmstat— if they’re consistently non-zero, upgrade RAM. - NVMe-based VPS plans handle emergency swap much better than SATA SSD or HDD plans.


Leave a Reply
You must be logged in to post a comment.