Hostxpeed
Login Get Started →
Server Management

How to Add More Swap Space

5 min read
25 views
Jun 10, 2026

Prerequisites

Before adding swap, make sure you have:

  • SSH access to your VPS
  • Root or sudo privileges
  • At least 10-20% free disk space

💡 Swap is disk space used as emergency RAM. It is slower but prevents crashes when physical RAM is full.

Step 1: Check Current Swap

ssh hxroot@YOUR_SERVER_IP -p 22
swapon --show
free -h

Step 2: Create a Swap File

To add 2GB of swap:

sudo fallocate -l 2G /swapfile

If fallocate not available:

sudo dd if=/dev/zero of=/swapfile bs=1M count=2048

Step 3: Set Correct Permissions

sudo chmod 600 /swapfile

Step 4: Format as Swap

sudo mkswap /swapfile

Step 5: Enable Swap

sudo swapon /swapfile

Step 6: Make Permanent (Survive Reboot)

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Step 7: Verify

free -h
swapon --show

How Much Swap to Add?

  • RAM ≤ 2GB → Swap = 2x RAM
  • RAM 2-8GB → Swap = same as RAM
  • RAM 8-64GB → Swap = 4-8GB
  • RAM > 64GB → Swap = 4GB minimum

Adjust Swappiness (How Often Swap is Used)

cat /proc/sys/vm/swappiness

Default is 60. Lower values reduce swap usage (good for SSDs):

sudo sysctl vm.swappiness=10

Make permanent:

echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf

Remove Swap (If Needed)

sudo swapoff /swapfile
sudo rm /swapfile

Remove the line from /etc/fstab.

✅ Swap space has been added successfully.

Was this article helpful?