Swap is space on the disk where the system moves data out of RAM when memory runs low. You'll add it in a few minutes, and thanks to it the VPS won't crash during a short spike. We'll also show when swap doesn't help and you need more RAM.
What you'll need
- A StormHost VPS with Ubuntu 24.04 or Debian 13
- An SSH connection as root (guide)
- Free disk space at least the size of the swap (check with
df -h /) - About 10 minutes
What swap is and when it helps#
When RAM runs out and there's no swap, the Linux kernel starts the OOM killer (“out of memory”) and kills the process that takes up the most memory. Usually that's exactly your database, game server or application. In the log you'll then find Out of memory: Killed process.
With swap, the system first moves rarely used data to the disk and the process keeps running.
| Situation | Does swap help? |
|---|---|
| A short spike (updates, compiling, a backup) | Yes, the VPS gets through it without crashing. |
| Data that's loaded once and then sits there (part of a database's memory, unused services) | Yes, it frees RAM for what matters. |
| An application permanently needs more RAM than the VPS has | No. The disk is many times slower than RAM, so the application slows down. The fix is a RAM upgrade. |
| A game server (Minecraft, Rust) with full memory | No. The server starts to lag. Give it less memory, or add RAM. |
Checking the current state#
swapon --show
free -hswapon --show– lists active swap. If it prints nothing, the VPS has no swap (that's normal for a new VPS).free -h– shows RAM and swap usage in readable units (-h= human readable).
In the free -h output, watch the available column on the Mem line: how much memory applications really have available. The Swap line shows the swap size and usage.
How big a swap to choose#
| VPS RAM | Recommended swap | Why |
|---|---|---|
| 2 GB | 2 GB | A small VPS hits the limit most easily, so the reserve comes in handy. |
| 4–8 GB | 2–4 GB | Enough to cover spikes. |
| 16 GB and more | 2–4 GB | A bigger swap makes no sense. If the VPS really uses it, it needs more RAM. |
Swap takes up disk space, so don't make it needlessly big.
Creating a swap file#
This applies to both Ubuntu 24.04 and Debian 13. As an example we'll create a 2 GB swap in the file /swapfile.
Create a file of the required size:
bash fallocate -l 2G /swapfilefallocate– quickly reserves disk space for a file.-l 2G– the file size (2G= 2 GB; for 4 GB write4G)./swapfile– the path to the file.
Set the permissions so that only root can read the file. Swap can contain chunks of memory with passwords:
bash chmod 600 /swapfile600– read and write only for the owner (root), nothing for others.
Prepare the file as swap:
bash mkswap /swapfilemkswap– writes a swap header to the file. It printsSetting up swapspace version 1, size = 2 GiB.
Turn swap on:
bash swapon /swapfileswapon– starts using the swap right away, but only until the next restart.
Check the result with
swapon --show. You'll see the line/swapfile file 2G.
When swapon reports “holes”
If swapon prints skipping - it appears to have holes, the file system didn't accept the file created with fallocate. Delete it (rm /swapfile) and create it more slowly but reliably with dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress (count=2048 = 2048 × 1 MB = 2 GB). Then continue with step 2.
Enabling it permanently through /etc/fstab#
To have swap turn on after a restart too, add it to the /etc/fstab file.
Back up the file:
bash cp /etc/fstab /etc/fstab.bakAdd a swap line to the end of the file:
bash echo '/swapfile none swap sw 0 0' >> /etc/fstabecho '…'– prints the text in quotes.>>– appends the text to the end of the file. Careful: a single>would overwrite the whole file.
What each column of the line means:
Column Value Meaning 1. device /swapfileThe swap file. 2. mount point noneSwap isn't mounted anywhere. 3. type swapIt's swap. 4. options swDefault options for swap. 5. dump 0Don't back up with the old dumptool.6. check 0Don't check at boot (swap is never checked). Check that fstab is fine:
bash findmnt --verifyfindmnt --verify– checks the syntax of/etc/fstab. The result must beSuccess, no errors or warnings detected, or a summary with0 parse errors, 0 errors. Warnings alone (warnings) about other lines won't stop the VPS from booting.
A broken /etc/fstab can stop the VPS from booting
If the VPS doesn't come up after a restart, open the browser console and restore the backup with cp /etc/fstab.bak /etc/fstab.
Swappiness: how eagerly the system swaps#
The vm.swappiness value (0–200) says how soon the kernel starts moving data to swap. The default is 60. On a server a lower value is better, so that swap is used mainly as a safety net.
| Setting | What it does | Recommended value | When to change |
|---|---|---|---|
vm.swappiness | The balance between freeing the file cache and moving process memory to swap. A lower number = less swapping. | 10 | Go back to 60 (the default) if the VPS is slower under full memory than before. The value 0 doesn't mean “swap off”; the system just uses it only in an emergency. |
Find the current value:
bash cat /proc/sys/vm/swappinessSave the new value to your own file (Debian 13 no longer reads
/etc/sysctl.conf; the/etc/sysctl.d/folder works on both systems):bash echo 'vm.swappiness=10' > /etc/sysctl.d/99-swappiness.confLoad the setting without a restart:
bash sysctl --systemsysctl --system– loads all settings from thesysctl.dfolders. In the output you'll see the linevm.swappiness = 10.
Verification#
swapon --showshows/swapfilewith the right size.free -hhas a non-zerototalvalue on theSwapline.- After a VPS restart, swap is active even without running
swaponby hand.
Removing swap#
- Turn swap off:
swapoff /swapfile. If swap is heavily used, it takes longer because the data moves back to RAM. If there isn't enough RAM for that, the command ends with an error and nothing happens. - Delete the line
/swapfile none swap sw 0 0from the/etc/fstabfile (nano /etc/fstab). - Delete the file:
rm /swapfile.
Troubleshooting#
fallocate: fallocate failed: No space left on device#
There isn't enough space on the disk. Check the free space with df -h / and choose a smaller swap, or extend the disk.
swapon: /swapfile: insecure permissions 0644, 0600 suggested.#
The file's permissions are too open. Swap turns on, but anyone on the VPS can read the file. Run chmod 600 /swapfile.
swapon: /swapfile: swapon failed: Device or resource busy#
Swap is already on. Check with swapon --show.
The VPS is slow even with swap and swap is always full#
The applications permanently need more memory than the VPS has. Find out what's using memory (htop, sort by the MEM% column) and either give the application less memory or add RAM in the Change plan tab.
Summary
- Swap protects against crashes during a short RAM shortage, but it's no substitute for RAM.
- Creating it:
fallocate,chmod 600,mkswap,swapon. - Permanently: the line
/swapfile none swap sw 0 0in/etc/fstaband a check withfindmnt --verify. - On a server, set
vm.swappiness=10in/etc/.sysctl. d/ 99-swappiness. conf - Full swap and a slow VPS mean you need more RAM.