Log inSign up

Give a small server a safety margin with a swap file

Allocate a file with sudo fallocate -l 2G /swapfile (or dd on XFS), lock it down with chmod 600, format it with mkswap, turn it on with swapon and add one line to /etc/fstab so it returns after a reboot. Then lower vm.swappiness to around 10 so the kernel keeps active data in RAM. swapon --show and free -h prove it is in use.

Topic: Server basics · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9, FreeBSD 14 · Updated 2026-12-03

What does a swap file do on a VPS?

Swap is disk space the kernel can use to park memory pages that have not been touched for a while. On a 1 GB or 2 GB instance it turns a sudden memory spike, for example a package upgrade, a compiler run or a database import, from an out of memory kill into a short slowdown. It is not a replacement for RAM: when an application keeps swapping actively, response times climb sharply, even on NVMe storage.

Our images do not create a swap area during deploy, so a new server starts with none. You can confirm that with swapon --show, which prints nothing when no swap is active. The first SSH session tutorial shows a quick one gigabyte file; this guide covers sizing, the XFS case, tuning, resizing and FreeBSD.

How big should the swap file be?

Size it as a buffer, not as extra memory. A practical rule for servers:

  • 1 GB of RAM: 1 to 2 GB of swap
  • 2 to 4 GB of RAM: 2 GB of swap
  • 8 GB or more: 2 to 4 GB, or none if your workload is predictable

Hibernation, which is the classic reason for swap equal to RAM, does not apply to a cloud server. Check free disk space before you start, because the swap file takes its full size immediately:

df -h /
free -h
swapon --show

If your workload regularly fills both RAM and swap, the cure is more memory: deploy a larger plan and follow move to a larger plan, since plans cannot be resized in place.

How do I create and enable the swap file?

On ext4, the root file system on our Ubuntu and Debian images, fallocate reserves the space instantly:

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

Rocky Linux 9 and AlmaLinux 9 use XFS for the root file system. Recent kernels accept fallocated swap files on XFS, but writing real zeros with dd always works and is the method the RHEL documentation uses, so it is the safer choice there:

sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

If swapon ever reports that the file has holes, delete it and recreate it with dd. The chmod 600 step is important: swap can contain fragments of passwords and keys, so no other user should be able to read it.

How do I keep swap after a reboot?

Add an entry to /etc/fstab. Make a quick copy of the file first, since a broken fstab can stop the boot and there is no browser console to repair it:

sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab
sudo findmnt --verify
sudo swapoff /swapfile && sudo swapon -a
swapon --show

findmnt --verify parses the whole file and reports mistakes before they matter. Turning the swap off and enabling everything with swapon -a proves the new line works exactly as it will during boot. After the next planned reboot, run swapon --show once more to be sure.

How do I tune swappiness and cache pressure?

Lower vm.swappiness so the kernel prefers dropping file cache over pushing application memory to disk. The default is 60; servers usually run well between 10 and 20.

cat /proc/sys/vm/swappiness
sudo sysctl vm.swappiness=10
printf 'vm.swappiness=10\nvm.vfs_cache_pressure=50\n' | sudo tee /etc/sysctl.d/90-swap.conf
sudo sysctl --system | grep -E 'swappiness|cache_pressure'

The file in /etc/sysctl.d/ applies the values at every boot. vfs_cache_pressure set to 50 asks the kernel to keep directory and inode caches a little longer, which helps servers that walk many small files. Database hosts sometimes go as low as 1 for swappiness; a value of 0 does not disable swap but makes the kernel use it only under real pressure.

How can I see whether swap is actually being used?

Watch the totals and the swap in and out rates. A full swap file is not a problem by itself; constant traffic in both directions is.

free -h
vmstat 5 5
cat /proc/pressure/memory

In the vmstat output, the si and so columns show pages swapped in and out per second. Values that stay above zero for minutes mean the server is short of memory. The pressure file shows how much time tasks spent waiting for memory. To find which processes hold swapped pages, see processes and memory, and the usage monitoring tutorial helps you follow trends over days.

How do I resize or remove the swap file?

Turn the file off, change it and turn it back on. Make sure there is enough free RAM to absorb what is currently swapped out, otherwise swapoff fails or the OOM killer steps in.

free -h
sudo swapoff /swapfile
sudo rm /swapfile
sudo fallocate -l 4G /swapfile   # or the dd command on XFS
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

The fstab line stays the same because the path has not changed. To remove swap for good, run swapoff, delete the file and delete the /swapfile line from /etc/fstab, then confirm with sudo findmnt --verify.

How do I add swap on FreeBSD 14?

FreeBSD attaches a swap file through a memory disk. Create the file, then let fstab describe it so it is enabled late in the boot:

dd if=/dev/zero of=/usr/swap0 bs=1m count=2048
chmod 0600 /usr/swap0
echo 'md99 none swap sw,file=/usr/swap0,late 0 0' >> /etc/fstab
swapon -aL
swapinfo -h

Run these as root (or through doas or sudo if you installed one). swapon -aL activates the late entries right away, and swapinfo -h shows the new device as /dev/md99. On a ZFS root, swap on a zvol is possible but a plain file like this is simpler for a small server.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-1the size that benefits most from a swap file1 vCPU, 1 GB RAM, 25 GB NVMe$3.60/mo$43.20 for 12 months
A1-2more headroom, swap stays a safety net1 vCPU, 2 GB RAM, 50 GB NVMe$7.20/mo$43.20 for 6 months

Questions about Add a swap file

Does swap wear out the SSD or NVMe disk?

Light swapping causes very little writing. With swappiness at 10 the kernel only moves idle pages, which is far less traffic than a database or log files produce.

Should I use zram instead of a swap file?

zram compresses memory pages inside RAM instead of writing them to disk. It can help on very small instances, but it still uses memory. A modest swap file plus low swappiness is the simplest starting point; you can add zram later with the zram-generator package where your distribution provides it.

Why does swapon say the file has holes?

The file system did not allocate real blocks for it. Delete the file and create it again with dd if=/dev/zero, which writes every block.

Can I use a swap partition instead of a file?

Our images use the whole disk for the root file system, so a file is the practical option. Performance is the same on current kernels.

Why is swap used when plenty of RAM is free?

The kernel moved idle pages there earlier and has no reason to read them back until they are needed. Used swap with low si and so rates in vmstat is harmless.