Swap on a VPS: When You Need It and How to Add It

Swap on a VPS is disk space the Linux kernel uses as overflow parking for RAM. When your VPS runs out of physical memory, the kernel moves idle memory pages onto disk, freeing RAM for active work. On a small VPS with 1 or 2 GB of RAM, a modest swap file can be the difference between a smooth deployment and a database process killed without warning.

Swap covers short spikes; it does not replace RAM. Disk is dramatically slower than RAM, so heavy swapping makes a server crawl. This guide explains when swap on a VPS actually helps, when it only hides the real problem, and how to add, tune, and verify a swap file on Ubuntu or Debian without breaking anything.

What Swap Actually Does

Linux divides memory into pages. Pages holding program data and heap (anonymous pages) live in RAM while the program is active. When RAM fills up, the kernel writes pages that have not been touched recently to the swap area on disk, then reads them back when needed.

Only anonymous pages can go to swap. File-backed pages, such as cached file contents, are simply dropped and re-read from disk later. Swap gives the kernel a second place to park anonymous memory, which buys headroom during short spikes: a deploy running composer install, a backup compressing a large archive, or a traffic burst spawning extra PHP workers.

On a modern VPS with SSD storage, occasional swapping is tolerable. Sustained swapping is not: if your server lives in swap, every request waits on disk I/O and response times collapse.

Signs Your VPS Actually Needs Swap

Swap helps when memory pressure is brief and rare. Look for these signs:

  • The OOM killer fires during spikes. Check sudo dmesg | grep -i "out of memory" or sudo journalctl -k | grep -i oom. If the kernel killed MySQL, PHP-FPM, or your app during a deploy or backup, swap would have absorbed the spike.
  • RAM sits near full but the server is mostly idle. Run free -h. If available is close to zero while load is low, the kernel has nowhere to put transient allocations. A gigabyte or two of swap smooths those moments out.
  • Builds and package installs die on small instances. Tools like npm install or compiling from source briefly need far more memory than the running services do. Swap is cheaper than resizing the VPS for a five-minute job.
  • You want a buffer while you investigate. Swap keeps the server alive while you figure out whether the memory growth is a leak, a misconfigured worker pool, or genuine growth.

When Swap Is the Wrong Fix

Swap covers spikes. It does not fix chronic shortage:

  • The server swaps constantly. If vmstat 1 shows steady si and so activity all day, the workload genuinely needs more RAM. Swap just makes the slowness quieter.
  • A database lives mostly in swap. Database engines assume their buffer pool is in RAM. Parking it on disk doubles the I/O path. Size the buffer pool to fit RAM, then upgrade the plan.
  • You are masking a memory leak. A service whose memory grows forever will eventually fill swap too, then die anyway. Fix the leak or restart the service on a schedule; do not keep feeding it disk.

How Much Swap to Add

The old “twice your RAM” rule comes from the spinning-disk era and hibernation, neither of which applies to a typical VPS. These are starting points, not law; watch real usage with free -h and adjust:

VPS RAM Suggested swap Why
1 GB 1–2 GB Smallest plans hit spikes hardest; 2 GB covers most deploys
2 GB 2 GB Matches common Ubuntu installer default; enough for builds and backups
4 GB 2–4 GB Safety net for bursts; 2 GB is usually plenty
8 GB or more 0–4 GB Often fine with none; add 2–4 GB if you want OOM insurance

One more constraint: swap needs free disk space. A 2 GB swap file on a 25 GB disk is fine; on a nearly full disk, clear space first with df -h.

How to Add a Swap File on Ubuntu or Debian

A swap file is easier than repartitioning and works the same for our purposes. First, check the current state:

free -h
swapon --show
df -h /

If swapon --show prints nothing, there is no active swap. The example below adds 2 GB; adjust the size to match the table above.

Step 1: Create the file with fallocate (instant):

sudo fallocate -l 2G /swapfile

If your filesystem does not support fallocate for swap (btrfs with copy-on-write refuses), use dd instead. It is slower but works everywhere:

sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress

Step 2: Lock down permissions, because a swap file can hold fragments of process memory and must not be world-readable:

sudo chmod 600 /swapfile

Step 3: Format it as swap space:

sudo mkswap /swapfile

Step 4: Enable it immediately:

sudo swapon /swapfile

Step 5: Verify:

free -h
swapon --show

You should see the new swap in both outputs. It vanishes on the next reboot unless you make it persistent.

Step 6: Make it survive reboots. Append an entry to /etc/fstab:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Double-check the line landed correctly, because a typo in fstab can stall a boot:

cat /etc/fstab

Tuning Swappiness for a VPS

vm.swappiness controls how eagerly the kernel moves pages to swap. It ranges from 0 to 100, and Ubuntu defaults to 60. A high value means the kernel swaps out idle program memory early to keep more file cache; a low value means it holds program memory in RAM longer and swaps only when it must.

For a typical VPS running a web stack, 10 is a sensible starting point: the kernel keeps your app and database workers in RAM and touches swap only under real pressure. Check the current value and set it:

cat /proc/sys/vm/swappiness
sudo sysctl vm.swappiness=10

That change lasts until reboot. To keep it, write it to a sysctl drop-in:

echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swap.conf
sudo sysctl --system

Do not set swappiness to 0 unless you know why. On modern kernels 0 does not fully disable swap; it only makes the kernel avoid it until memory is nearly exhausted, which is rarely what a small VPS wants during a spike.

Verify Swap After a Reboot

Reboot once during a maintenance window and confirm everything came back:

sudo reboot

After the server returns:

free -h
swapon --show
cat /proc/sys/vm/swappiness

All three should reflect your new setup: swap listed, size correct, swappiness at 10. If swap is missing, re-read your /etc/fstab line for typos.

Removing Swap Later

If you upgrade to a bigger plan and no longer want swap, disable it, remove the fstab entry, and delete the file:

sudo swapoff /swapfile
sudo sed -i '//swapfile/d' /etc/fstab
sudo rm /swapfile

Run free -h afterwards to confirm swap reads zero. The sed command deletes only the line mentioning /swapfile.

Common Mistakes to Avoid

  • Skipping chmod 600. A world-readable swap file leaks whatever the kernel parked there. Set permissions before running mkswap.
  • Using fallocate on btrfs. Copy-on-write filesystems can produce a swap file the kernel rejects at swapon time. If swapon fails with an error about the file, delete it and recreate with dd.
  • Forgetting fstab. The classic support-forum post: “it worked until I rebooted.” The fstab line is the whole difference.
  • Treating swap as RAM. If free -h shows swap half-used every day, the workload needs more RAM or fewer services. Swap bought you time; spend it fixing the sizing.

Frequently Asked Questions

What is swap on a VPS?

Swap is disk space the Linux kernel uses as an overflow area for RAM. When physical memory fills up, idle memory pages move to the swap file, freeing RAM for active processes. It prevents crashes during short spikes but is far slower than RAM.

How much swap should a VPS have?

A practical starting point is 2 GB of swap on a 1–2 GB VPS and 2–4 GB on a 4 GB VPS. Monitor actual usage with free -h over a week and adjust; constant heavy swapping means you need more RAM, not more swap.

Does swap slow down a VPS?

Only when the server actually swaps heavily. Idle swap costs nothing. If the workload constantly reads and writes swap, every affected request waits on disk I/O, which is far slower than RAM. Occasional swapping during deploys or backups is normal.

Swap on a VPS is cheap insurance: a 2 GB file and one fstab line stand between a traffic spike and a dead database process. Add it, tune swappiness down, verify it survives a reboot, and watch usage. If swap becomes a daily crutch instead of a rare safety net, that is your signal to size up the plan. For the bigger picture, see how a VPS compares with shared hosting and dedicated servers before you resize. And if you run Docker services on the box, our Jellyfin media server setup is a good example of a stack whose memory spikes swap can absorb.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.