Swap is disk space the kernel uses when RAM is full, or to move rarely used memory pages out of the way. Linux can use either a dedicated swap partition or a swap file on an existing filesystem. On modern kernels the performance difference is negligible, so the decision is mostly about flexibility.
The short answer
- On a VPS or cloud server: use a swap file. You can create, resize or remove it in seconds without repartitioning.
- Use a partition if you need hibernation on some setups, run a filesystem where swap files are awkward, or your installer already created one.
Comparison
| Swap file | Swap partition | |
|---|---|---|
| Create / resize | Instantly, no reboot | Requires repartitioning |
| Performance | Practically identical on ext4 and XFS | Practically identical |
| Filesystem requirements | Works on ext4 and XFS; Btrfs needs special handling (no copy-on-write, no compression) | None |
| Hibernation | Possible but needs extra configuration | Simpler |
| Typical use | VPS, cloud instances, desktops | Traditional installs, hibernation laptops |
How much swap does a server need?
Old rules like “twice your RAM” came from machines with very little memory. For servers today:
| RAM | Suggested swap |
|---|---|
| 1–2 GB | 1–2 GB |
| 4–8 GB | 2–4 GB |
| 16 GB+ | 2–4 GB (a safety net, not extra memory) |
Swap is a safety net. If a server constantly uses swap under normal load, it needs more RAM or fewer workers — disk is thousands of times slower than memory.
Check your current swap
swapon --show
free -h
Create a swap file
fallocate -l 2G /swapfile # or: dd if=/dev/zero of=/swapfile bs=1M count=2048
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap sw 0 0' >> /etc/fstab
On Btrfs, use btrfs filesystem mkswapfile --size 2G /swapfile (kernel 6.1+ / recent btrfs-progs), which sets the right attributes.
Resize a swap file
swapoff /swapfile
fallocate -l 4G /swapfile
mkswap /swapfile
swapon /swapfile
Tune swappiness
vm.swappiness (0–200, default 60) controls how eagerly the kernel swaps. For servers running databases or application workers, a lower value keeps active memory in RAM:
sysctl vm.swappiness=10
echo 'vm.swappiness=10' > /etc/sysctl.d/90-swap.conf
Alternative: zram
zram creates a compressed swap device in RAM. Compressed pages often take a third of the space, so it’s like getting extra memory at the cost of some CPU. It’s useful on small VPS plans:
apt install -y zram-tools # Debian/Ubuntu
# /etc/default/zramswap: PERCENT=50
systemctl restart zramswap
You can combine zram (higher priority) with a small disk swap file as a fallback.
Watch for memory pressure
vmstat 1 5 # si/so columns: pages swapped in/out per second
cat /proc/pressure/memory # PSI: time spent waiting for memory
Occasional swapping is fine. Constant si/so activity means the server is short of RAM — see Debian performance tuning or consider a larger plan. Our KVM VPS plans run their own kernel, so you can configure swap and zram freely.