Check what you have
free -h
swapon --show
If swapon prints nothing, there is no swap configured.
How much to add
| RAM | Suggested swap |
|---|---|
| 2 GB | 2 GB |
| 4 GB | 2 GB |
| 8 GB and above | 1 to 4 GB |
Swap is not a substitute for memory. The aim is to absorb brief peaks, package upgrades and memory leaks long enough for you to notice, not to run a workload that does not fit.
1. Create the swap file
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
If fallocate is unsupported on your filesystem, create the file with sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 instead. The chmod step matters: swap can contain fragments of any process's memory, including secrets.
2. Make it permanent
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Reboot once and confirm with swapon --show that it comes back.
3. Tune swappiness
vm.swappiness controls how eagerly the kernel moves pages to swap. The default of 60 suits desktops. For servers, a lower value keeps active data in RAM:
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swap.conf
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.d/99-swap.conf
sudo sysctl --system
vfs_cache_pressure=50 makes the kernel hold on to filesystem metadata a little longer, which helps servers with many small files.
Watching swap in use
vmstat 5
Watch the si and so columns (swap in and swap out per second). Occasional small numbers are fine. Sustained non-zero values mean the server is actively short of memory, and performance will suffer because even SSD is far slower than RAM. The memory graph in the VeloxMedia control panel shows the same trend over days and weeks.
When to resize instead
Swap is hiding a sizing problem if:
siandsostay above zero during normal traffic- Used swap keeps growing day after day
- The out-of-memory killer still appears in
journalctl -k | grep -i oom
At that point move up a plan from the portal. Going from Micro to Small doubles memory for $2 a month, and your disk, data and IP address stay the same. Our sizing guide shows how to tell memory pressure from CPU pressure.
Removing swap
sudo swapoff /swapfile
sudo sed -i '\#/swapfile#d' /etc/fstab
sudo rm /swapfile