Apply for Free VPS

Add swap space

Updated 27 Nov 2026 swap memory

Add a swap file so short memory spikes do not stop your programs.

Swap is disk space the system can use when memory is tight. It is far slower than memory, especially on a hard disk, so treat it as a safety net.

Check

swapon --show
free -h

Create a 2 GB swap file

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

If swapon rejects the file, recreate it with sudo dd if=/dev/zero of=/swapfile bs=1M count=2048.

Keep it after a restart

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

Use it only when needed

echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swap.conf
sudo sysctl --system

When swap is a symptom

If vmstat 5 shows steady swapping in and out, the server needs less load or more memory. See memory pressure.

Remove it

sudo swapoff /swapfile
sudo rm /swapfile

Delete the line from /etc/fstab as well.

Was this helpful?

Report outdated or incorrect information