How to Configure Swap on Linux — VPSGrid Knowledge Base
English
EnglishBeta EspañolPortuguês (Brasil)Coming soonFrançaisComing soonDeutschComing soon
Client Area

How to Configure Swap on Linux

KB-056, Linux VPS, VPSGrid, unmanaged 0 found helpful

How to Configure Swap on Linux

Short answer

Create a swap file (common on VPS disks), set permissions, mark it with mkswap, enable it with swapon, add it to /etc/fstab, then optionally tune vm.swappiness. Swap is emergency overflow — it is slower than RAM.

Overview

Swap helps small VPS plans survive short memory spikes. It does not replace upgrading RAM if you are constantly swapping. VPSGrid Linux VPS plans are unmanaged — you configure swap inside the guest.

When swap helps

  • Occasional spikes (builds, apt/dnf, bursts of traffic)
  • Preventing the OOM killer from immediately killing processes on low-RAM plans

If the disk is thrashing continuously, upgrade the plan instead of adding huge swap.

1) Check current memory and swap

free -h
swapon --show
df -h /

Confirm you have enough free disk for the swap file you want (example below uses 2G).

2) Create a 2G swap file

Run as root (or with sudo). Adjust size if needed:

fallocate -l 2G /swapfile || dd if=/dev/zero of=/swapfile bs=1M count=2048
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
swapon --show
free -h
Note

If fallocate fails on some filesystems, the dd fallback above still works. Never make /swapfile world-readable.

3) Make swap permanent

Add to /etc/fstab (one line):

echo '/swapfile none swap sw 0 0' | tee -a /etc/fstab
# verify
tail -n 3 /etc/fstab
# safe test without reboot
swapoff /swapfile && swapon -a && swapon --show

4) Optional: swappiness

Lower values prefer RAM longer before swapping (common on servers):

# temporary
sysctl vm.swappiness=10

# permanent
echo 'vm.swappiness=10' | tee /etc/sysctl.d/99-swappiness.conf
sysctl --system | grep swappiness

5) Remove swap later (if needed)

swapoff /swapfile
# remove the /swapfile line from /etc/fstab
rm /swapfile

Frequently Asked Questions

How big should swap be?

For many small VPS workloads, 1–2× RAM is a common starting point, capped by free disk. Prefer upgrading RAM if swap is busy all the time.

Swap partition vs swap file?

A swap file is usually easier on VPS images without a dedicated swap partition. Performance difference is typically small for this use case.

Will swap speed up my site?

No. Swap prevents crashes under pressure; active use of swap is slower than RAM. Optimize the app or upgrade the plan for sustained load.

Related Articles

Need More Help?

Open a ticket at billing.vpsgrid.net with your service ID, free -h output, and the exact error from mkswap/swapon.

Was this answer helpful?

Related Articles