Skip to content
VeloxMedia
VPS guide

Add Swap Space to a Linux VPS

Swap gives the kernel somewhere to put rarely used memory pages so a short spike slows the server down instead of killing a process. On small servers it is cheap insurance; on any server it is a useful early warning.

Beginner · About 3 min read · Updated 27 October 2026

Check what you have

free -h
swapon --show

If swapon prints nothing, there is no swap configured.

How much to add

RAMSuggested swap
2 GB2 GB
4 GB2 GB
8 GB and above1 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:

  • si and so stay 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

Common questions

Does a VPS need swap?

It is not required, but a modest swap file is cheap insurance on servers with 4 GB of RAM or less. It lets the kernel absorb short memory spikes instead of killing processes.

How much swap should I add to a 2 GB VPS?

About 2 GB is a sensible amount. Swap should cover brief peaks; if the server relies on it constantly, move to a plan with more memory.

What swappiness value is best for a server?

A value around 10 keeps active data in RAM and only swaps under real memory pressure. The default of 60 is tuned for desktops.

Is swap on SSD fast enough to replace RAM?

No. SSD is much faster than older disks but still far slower than memory. Heavy swapping will slow a server noticeably, which is a sign to resize the plan.

Deploy your first server in under a minute

Pick a region, choose a size and connect over SSH. No setup fees, cancel anytime.