Skip to content
Current coupon:10% offUse it in your order

Swap on a VPS: when to add it and how

What swap is, when it helps and when it doesn't. Create a swap file, enable it permanently through /etc/fstab, set swappiness and learn how to remove swap again.

  • beginner
  • ~10 min
  • Updated 1 Oct 2026

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.

SituationDoes 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 hasNo. 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 memoryNo. The server starts to lag. Give it less memory, or add RAM.

Checking the current state#

bash
swapon --show
free -h
  • swapon --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 RAMRecommended swapWhy
2 GB2 GBA small VPS hits the limit most easily, so the reserve comes in handy.
4–⁠8 GB2–⁠4 GBEnough to cover spikes.
16 GB and more2–⁠4 GBA 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.

  1. Create a file of the required size:

    bash
    fallocate -l 2G /swapfile
    • fallocate – quickly reserves disk space for a file.
    • -l 2G – the file size (2G = 2 GB; for 4 GB write 4G).
    • /swapfile – the path to the file.
  2. Set the permissions so that only root can read the file. Swap can contain chunks of memory with passwords:

    bash
    chmod 600 /swapfile
    • 600 – read and write only for the owner (root), nothing for others.
  3. Prepare the file as swap:

    bash
    mkswap /swapfile
    • mkswap – writes a swap header to the file. It prints Setting up swapspace version 1, size = 2 GiB.
  4. Turn swap on:

    bash
    swapon /swapfile
    • swapon – starts using the swap right away, but only until the next restart.
  5. 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.

  1. Back up the file:

    bash
    cp /etc/fstab /etc/fstab.bak
  2. Add a swap line to the end of the file:

    bash
    echo '/swapfile none swap sw 0 0' >> /etc/fstab
    • echo '…' – 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:

    ColumnValueMeaning
    1. device/swapfileThe swap file.
    2. mount pointnoneSwap isn't mounted anywhere.
    3. typeswapIt's swap.
    4. optionsswDefault options for swap.
    5. dump0Don't back up with the old dump tool.
    6. check0Don't check at boot (swap is never checked).
  3. Check that fstab is fine:

    bash
    findmnt --verify
    • findmnt --verify – checks the syntax of /etc/fstab. The result must be Success, no errors or warnings detected, or a summary with 0 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.

SettingWhat it doesRecommended valueWhen to change
vm.swappinessThe balance between freeing the file cache and moving process memory to swap. A lower number = less swapping.10Go 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.
  1. Find the current value:

    bash
    cat /proc/sys/vm/swappiness
  2. Save 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.conf
  3. Load the setting without a restart:

    bash
    sysctl --system
    • sysctl --system – loads all settings from the sysctl.d folders. In the output you'll see the line vm.swappiness = 10.

Verification#

  • swapon --show shows /swapfile with the right size.
  • free -h has a non-zero total value on the Swap line.
  • After a VPS restart, swap is active even without running swapon by hand.

Removing swap#

  1. 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.
  2. Delete the line /swapfile none swap sw 0 0 from the /etc/fstab file (nano /etc/fstab).
  3. 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 0 in /etc/fstab and a check with findmnt --verify.
  • On a server, set vm.swappiness=10 in /etc/sysctl.d/99-swappiness.conf.
  • Full swap and a slow VPS mean you need more RAM.

Search the guides