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

Swap space is a critical but often overlooked part of VPS performance. When your VPS runs out of physical RAM, the kernel moves idle memory pages to swap — preventing out-of-memory crashes but at the cost of slower access times. Proper swap configuration keeps your server stable without sacrificing performance.

You can compare VPS plans with enough RAM for your workload on our VPS comparison page.

How Much Swap Do You Need?

RAM SizeRecommended SwapWith Hibernation
512 MB1 GB1 GB
1 GB1 GB2 GB
2 GB1 GB3 GB
4 GB2 GB5 GB
8 GB+2 GBRAM × 1.5

Creating Swap on a Linux VPS

Modern Linux systems use a swap file, not a swap partition. This is easier to resize and manage:

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

# Set correct permissions
sudo chmod 600 /swapfile

# Format as swap
sudo mkswap /swapfile

# Enable it
sudo swapon /swapfile

# Add to fstab for persistence
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

# Verify
sudo swapon --show
free -h

Swap Performance Optimization

The swappiness parameter controls how aggressively the kernel uses swap. A value of 10 means the kernel only swaps when RAM is over 90% full — ideal for VPS environments:

# Check current value
cat /proc/sys/vm/swappiness

# Set to 10 (only swap when necessary)
sudo sysctl vm.swappiness=10

# Make permanent
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

Monitoring Swap Usage

  • Use free -h for a quick overview
  • vmstat 5 shows swap activity in real-time (si = swap in, so = swap out)
  • High si/so values mean your RAM is insufficient — consider upgrading

Check our VPS hosting comparison for plans with adequate RAM for your workload.

Leave a Reply