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 Size | Recommended Swap | With Hibernation |
|---|---|---|
| 512 MB | 1 GB | 1 GB |
| 1 GB | 1 GB | 2 GB |
| 2 GB | 1 GB | 3 GB |
| 4 GB | 2 GB | 5 GB |
| 8 GB+ | 2 GB | RAM × 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 -hSwap 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.confMonitoring Swap Usage
- Use
free -hfor a quick overview vmstat 5shows 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
You must be logged in to post a comment.