On this page
- What does a swap file do on a VPS?
- How big should the swap file be?
- How do I create and enable the swap file?
- How do I keep swap after a reboot?
- How do I tune swappiness and cache pressure?
- How can I see whether swap is actually being used?
- How do I resize or remove the swap file?
- How do I add swap on FreeBSD 14?
- Questions
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 --showIf 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 --showRocky 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 /swapfileIf 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 --showfindmnt --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/memoryIn 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 /swapfileThe 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 -hRun 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.
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.
More in Server basics
- Create a sudo user
- Change the hostname
- Time zone and time sync
- File and directory permissions
- Choose a Linux distribution
- Upgrade Ubuntu to the next LTS
- Upgrade Debian to a new major release
- Rocky or AlmaLinux to a new major version
- Automatic security updates
- Run an app as a systemd service
- Schedule jobs with cron and systemd timers
- Environment variables on Linux
- Check disk space and find what fills it
- Inspect processes and memory
- Read the hardware and system report
- Reboot and shut down safely
- Customise the login message (MOTD)
- Remote desktop with Xfce and xrdp
- SELinux on Rocky Linux and AlmaLinux
- Keep sessions alive with tmux
- SFTP-only users in a chroot
- apt and dnf package management
- First-hour checklist for a new server
- Upgrade FreeBSD to a new release