Docs / Server Management / Setting Up a Swap File on Your VPS

Setting Up a Swap File on Your VPS

By Admin · Feb 25, 2026 · Updated Apr 23, 2026 · 69 views · 1 min read

What Is Swap?

Swap is disk space used as overflow when physical RAM is exhausted. While much slower than RAM, it prevents out-of-memory crashes and process kills.

Check Current Swap

free -h
swapon --show

Create a Swap File

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

# Set correct permissions
sudo chmod 600 /swapfile

# Format as swap
sudo mkswap /swapfile

# Enable swap
sudo swapon /swapfile

# Verify
free -h

Make It Permanent

Add to /etc/fstab:

/swapfile none swap sw 0 0

Tune Swappiness

Swappiness controls how aggressively Linux moves data from RAM to swap (0-100):

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

# Set temporarily
sudo sysctl vm.swappiness=10

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

Recommended Values

ValueBehaviorBest For
1-10Minimal swappingDatabase servers, SSD storage
30-40Moderate swappingGeneral purpose servers
60 (default)BalancedDesktop systems

Swap Size Guidelines

RAMRecommended Swap
1 GB2 GB
2 GB2-4 GB
4-8 GB2-4 GB
16+ GB2 GB (safety net)

Remove Swap

sudo swapoff /swapfile
sudo rm /swapfile
# Remove the /etc/fstab entry

Was this article helpful?