Skip to main content
Swap is a file on disk that the system uses as spare memory. Without it, a process that runs out of RAM gets killed by the OOM-killer and the service crashes. Disk is slower than memory: if the server lives in swap all the time, it needs a bigger plan.

Check

  • free -h – the Mem (RAM) and Swap rows.
  • swapon --show – empty output means there is no swap.

Create

Connect over SSH as root: Connection.
  1. File – 2 GB:
    No fallocate – do the same with dd, count=2048 with bs=1M gives 2 GB:
  2. Permissions – root only, otherwise the system refuses the file:
  3. Format and enable:
    Check: swapon --show and free -h show the swap.
  4. At boot – a line in /etc/fstab:
    There must be only one such line, check: grep swapfile /etc/fstab. Duplicates break the boot.

How much

  • 1–2 GB RAM – swap equal to RAM.
  • 4–8 GB RAM – 2–4 GB.
  • 16 GB RAM or more – 2–4 GB is enough.

Swappiness

vm.swappiness from 0 to 100: how eagerly the kernel moves pages to swap. The usual value is 60, 10 suits a server better: swap kicks in only when memory really runs out.
  • Current value:
  • Apply now:
  • Keep after reboot:
Memory runs out all the time – do not grow the swap, take a bigger plan: Upgrade on the server card. Next: LLM on the server, VPS not working.