VPS Swap Space: How to Configure and Optimize Swap on Linux VPS

Swap space acts as emergency overflow memory on a Linux VPS. When your server runs out of physical RAM, the kernel moves inactive memory pages to swap on disk, preventing the Out-Of-Memory (OOM) killer from terminating your applications. Properly configuring swap is especially important on low-RAM VPS plans where memory is tight and workloads can spike unpredictably. Compare VPS plans on our site to see which ones offer enough RAM to minimize swap dependency.

How Much Swap Should a VPS Have?

The old rule of thumb — “swap should be 2× RAM” — no longer applies to modern VPS environments. SSDs make swap faster than spinning disks, but swap is still orders of magnitude slower than RAM. For Linux VPS servers, use these guidelines:

Physical RAMRecommended Swap SizeUse Case
512 MB – 1 GB1 GB – 2 GBBudget VPS, very swap-dependent
2 GB1 GB – 2 GBGeneral web hosting with occasional spikes
4 GB1 GB – 2 GBSufficient RAM for most workloads
8 GB+512 MB – 1 GBSwap as a safety net only

If your VPS consistently uses more than 50% of swap, you need more RAM, not more swap. Adding swap space does not fix a RAM shortage — it only delays the inevitable performance degradation.

Checking Current Swap Usage

Before modifying swap, check what is already configured:

# Show swap summary
swapon --show

# Or use free -h for a memory overview
free -h

# Check how much swap is actually in use
cat /proc/meminfo | grep -i swap

These commands tell you whether swap is enabled, how large it is, and how much is being actively used. If swapon --show returns nothing, you have no swap configured at all.

Creating a Swap File on Linux VPS

Modern Linux systems use a swap file (not a swap partition), which is easy to resize. Here is how to add a 2 GB swap file on Ubuntu or Debian:

# 1. Create a 2 GB swap file
fallocate -l 2G /swapfile

# 2. Set correct permissions (root only)
chmod 600 /swapfile

# 3. Format as swap
mkswap /swapfile

# 4. Enable swap
swapon /swapfile

# 5. Make permanent (adds to /etc/fstab)
echo '/swapfile none swap sw 0 0' | tee -a /etc/fstab

Verify with swapon --show and free -h. The new swap should appear in both outputs immediately.

Tuning the Swappiness Parameter

The kernel parameter vm.swappiness controls how aggressively the system swaps memory pages. Values range from 0 to 100:

  • 0: Only swap when absolutely necessary (OOM risk).
  • 10: Good for VPS — avoids swap unless memory pressure is real.
  • 60: Default — conservative but may swap unnecessarily on low-RAM VPS.
  • 100: Aggressive swapping — rarely useful outside embedded systems.

Check your current value with cat /proc/sys/vm/swappiness. Change it temporarily with sysctl vm.swappiness=10, or permanently by adding vm.swappiness=10 to /etc/sysctl.conf and running sysctl -p.

Resizing or Removing Swap

To resize an existing swap file:

# Turn off swap
swapoff /swapfile

# Remove old file
rm /swapfile

# Create new file with desired size (e.g., 4 GB)
fallocate -l 4G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

Make sure you have enough free RAM before running swapoff — all pages currently in swap need to be moved back into physical memory, which can temporarily spike RAM usage.

Swap on SSD vs. HDD VPS

If your VPS uses NVMe or SATA SSDs, swap performance is acceptable for occasional use. On HDD-based VPS, swap is painfully slow — a single random I/O operation takes ~10 ms on HDD vs. ~0.1 ms on SSD. Avoid relying on swap with HDD storage; upgrade RAM instead. Most modern VPS providers use SSDs exclusively, but it is worth verifying with your provider before purchasing a budget plan.

Proper swap configuration is a simple but impactful optimization for any Linux VPS. Set a reasonable swap size, tune swappiness to 10, and monitor usage with free -h during peak traffic. If swap usage stays above 25% consistently, check out VPS plans on our site with more RAM to eliminate the bottleneck entirely.

Leave a Reply