VPS Swap Space Optimization: When and How to Configure Swap for Better Performance

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 -h shows 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 RAMRecommended SwapNotes
512 MB – 1 GB1-2 GBLow-RAM VPS benefits more from swap as a safety net
2 GB1-2 GBUseful for occasional spikes; consider 1 GB to start
4 GB1 GBMost workloads fit in RAM; swap is just insurance
8 GB512 MB – 1 GBOnly needed for very rare OOM scenarios
16 GB+0-512 MBDisable 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 ValueBehaviorBest For
0Swap only when absolutely necessary (OOM imminent)Databases (PostgreSQL, MySQL), Redis, in-memory caches
1Minimal swapping — slight preference for keeping pages in RAMWeb servers, application servers, most production workloads
10Conservative — swap idle processes but keep active ones in RAMGeneral-purpose VPS with mixed workloads
60 (default)Balance between RAM and swap usageDesktop, development servers, low-memory systems
100Aggressive swapping — kernel treats RAM and swap equallyAlmost 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 the si (swap in) and so (swap out) columns. If these are consistently above zero, your server is actively swapping.
  • htop — Each process shows its swapped memory percentage in the SWAP column.

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=1 for database servers, 10 for general VPS workloads.
  • Monitor si and so columns in vmstat — 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