Quick answer: Create a swap file with fallocate, secure it, format and enable it, then persist it in /etc/fstab. Swap gives a small VPS breathing room against out-of-memory crashes — but it is a safety net, not a substitute for enough RAM.

Overview

Swap is disk space the kernel uses when RAM runs low. On SSD/NVMe VPS plans it is fast enough to absorb short memory spikes (package upgrades, build steps, traffic bursts). If your server swaps constantly, the real fix is a RAM upgrade.

Before you start

  • Root/sudo access and free disk space (check first).
  • Check existing swap: swapon --show and free -h.

Step-by-step guide

  1. Create the file (2G example — typically 1–2× RAM for small servers):
    sudo fallocate -l 2G /swapfile
    sudo chmod 600 /swapfile
  2. Format and enable:
    sudo mkswap /swapfile
    sudo swapon /swapfile
  3. Persist across reboots:
    echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
  4. Tune how eagerly the kernel swaps (lower = prefer RAM; 10 is a good server value):
    sudo sysctl vm.swappiness=10
    echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
  5. Verify: free -h now shows the swap line.

Common issues

  • fallocate fails on some filesystems: use sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 instead.
  • Server is slow and swap is full: you are memory-starved — find the hog with top (sort by %MEM) and upgrade RAM.
  • "swapfile has holes" error: recreate it with dd as above.

When to contact support

If memory pressure is chronic, a plan upgrade is the sustainable fix — open a ticket or use Upgrade/Downgrade on your service page.

Frequently asked questions

How much swap should a VPS have?

A practical rule for small servers is one to two times RAM, e.g. a 2 GB swap file on a 2 GB VPS. It absorbs spikes; chronic swapping means you need more RAM.

Does swap wear out or slow down an SSD VPS?

Occasional swapping on SSD or NVMe storage is fast and harmless. Constant heavy swapping is a performance signal to reduce memory usage or upgrade the plan.

Why is my server slow even though swap is enabled?

If swap is heavily used, the workload no longer fits in RAM and disk becomes the bottleneck. Find the memory hog with top sorted by %MEM and upgrade RAM.

Related articles

Need a hand? Contact Cloud2Y support →

Was this answer helpful? 0 Users Found This Useful (0 Votes)