What I Wanted to Do

My VPS only had 1GB of RAM, and running Docker or Node.js would trigger the OOM Killer and kill my processes. I needed to add swap space to relieve the memory pressure.

What Is Swap?

Swap is a mechanism that uses part of the disk as a substitute for RAM when physical memory runs out. It’s slower than RAM, but much better than having processes killed unexpectedly.

Create a Swap File

# Create a 2GB swap file (bs=1M × count=2048)
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048

# Set permissions (readable/writable by root only)
sudo chmod 600 /swapfile

# Format it as swap space
sudo mkswap /swapfile

Enable the Swap

# Enable swap
sudo swapon /swapfile

# Confirm it's active
sudo swapon --show

You should see output like this:

NAME      TYPE SIZE USED PRIO
/swapfile file   2G   0B   -2

Check Swap Usage

# Memory and swap usage overview
free -h
              total        used        free      shared  buff/cache   available
Mem:          980Mi       400Mi       100Mi        10Mi       480Mi       450Mi
Swap:         2.0Gi         0B       2.0Gi
# More detailed info
cat /proc/meminfo | grep -i swap

Make Swap Persist After Reboot

Without this step, swap will be disabled after a reboot. Add an entry to /etc/fstab to make it permanent.

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

Verify the entry was added:

cat /etc/fstab | grep swap
/swapfile none swap sw 0 0

Tune swappiness

swappiness controls how aggressively the kernel uses swap (range: 0–100). VPS servers often default to 60, but a lower value is recommended for server workloads.

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

# Change temporarily (resets on reboot)
sudo sysctl vm.swappiness=10

# Persist the change
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

Remove Swap When No Longer Needed

# Disable swap
sudo swapoff /swapfile

# Delete the file
sudo rm /swapfile

# Also remove the line from /etc/fstab
sudo vim /etc/fstab

Common Pitfalls

  • Forgetting chmod 600 will cause mkswap to fail
  • Without the /etc/fstab entry, swap disappears every time you reboot
  • A good swap size is 1–2x your RAM (e.g., 2GB swap for a 1GB RAM VPS)
  • On SSD-based VPS, lower swappiness to around 10 to reduce write wear
  • If the Swap line in free -h still shows 0, swapon did not succeed

Looking for reliable cloud infrastructure? Check out these developer-friendly services.