How to Create a Swap File on a Low-RAM VPS and When It Helps

A 1GB VPS runs a small WordPress site, a mail server, or a side project without much trouble until one busy minute pushes it past its memory limit. The kernel then kills a process, often PHP-FPM or MySQL, and the site goes down. A swap file on a low-RAM VPS gives the system somewhere to put idle memory pages instead of killing something useful. It costs nothing, takes about five minutes to set up, and on a small server it is often the difference between a site that survives a traffic spike and one that does not.

Swap is not extra RAM. It is disk space the kernel treats as overflow memory, and disk is far slower than RAM. Used well, it keeps a server alive through short peaks; used badly, it turns a fast server into a slow one.

What a swap file on a low-RAM VPS actually does

When RAM fills up, the Linux kernel moves pages that have not been touched in a while to swap, freeing real RAM for whatever is active right now. A web server handling a burst of requests, for example, can keep its workers alive while idle background pages sit on disk for a few seconds.

Swap also helps the kernel avoid the out-of-memory killer. Without swap, a sudden spike leaves the kernel one option: kill a process. With swap, the process slows down instead of dying, which is the better outcome for a small site.

The catch: if the server swaps constantly, every request waits on disk. That is called thrashing, and it leaves the server slower than it would be with less memory and no swap. Swap works as emergency headroom, not as a permanent second helping of RAM. If your server leans on swap all day, cut memory use or buy more RAM instead of adding a bigger file.

When swap helps, and when it does not

Swap helps most on a 1GB or 2GB VPS running a stack with occasional peaks: a WordPress site that gets a burst after a post goes out, a backup job that runs at 3 a.m., or an apt upgrade that briefly needs more memory than usual. In each case the peak lasts seconds or minutes, and swap absorbs it.

Swap does not help a server that is short on RAM all the time. A database needing 1.5GB on a 1GB VPS will swap constantly, and response times will crawl. It also does not help a runaway process with a memory leak: swap just delays the crash while the leak fills the disk file too.

One more limit: some cheap VPS plans run on containers (OpenVZ or LXC) rather than full virtual machines, and the host controls swap there, so you usually cannot enable your own. Run swapon --show and free -h first. If swapon reports permission errors, check with your provider. On a KVM VPS, which most providers sell today, the steps below work as written.

How much swap to add

Old advice said to match RAM one-to-one, or double it. That rule comes from the days of 256MB servers, and it is too much for a modern VPS: disk space is limited on small plans, and a huge swap file invites the kernel to lean on it too much.

A practical starting point:

RAM Suggested swap file Notes
512MB 1GB Very tight; also cut services
1GB 1GB The common case; this swapfile size for 1gb vps setup is a safe default
2GB 2GB Headroom for peaks without wasting disk
4GB 2GB Past 4GB, emergencies only

If you want a second opinion before picking a size, our guide to when to enable swap and how much to allocate walks through the same sizing question in more detail, including when a smaller file is the right call.

How to create a swap file on a low-RAM VPS

The steps below assume Ubuntu 22.04 or 24.04, but they work on Debian and most other distributions with the same commands. Run them as root or with sudo.

Step 1: Check current memory and swap

free -h
swapon --show

If swapon --show prints nothing, no swap is active. Note the output of free -h so you can compare it after the change.

Step 2: Create the file

For a 1GB file, the fastest method is fallocate:

sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile

Some filesystems refuse fallocate for swap. If you see an error about the file not being supported, delete it and use dd instead; it writes every byte, so it is slower, but it works everywhere:

sudo rm /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024 status=progress
sudo chmod 600 /swapfile
sudo mkswap /swapfile

The chmod 600 step matters. Swap files with loose permissions are a security hole, and newer kernels refuse to activate them.

Step 3: Turn it on

sudo swapon /swapfile
swapon --show
free -h

free -h should now show 1.0G in the Swap row. If it shows 0B used, that is normal: the kernel only touches swap when RAM pressure appears.

Step 4: Make it survive a reboot

Add the file to /etc/fstab so it activates at boot:

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

Test the entry without rebooting by running sudo swapoff /swapfile && sudo swapon -a. If that returns without errors, the fstab line is correct. A typo here can stall the boot, so do not skip this check.

Step 5: Tune swappiness

The swappiness setting linux admins argue about most is vm.swappiness, a number from 0 to 100 that tells the kernel how eagerly to move pages to swap. The default of 60 suits desktops, but on a server you usually want the kernel to hold on to RAM longer and use swap only when it must.

sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

Start at 10 for a web server. If the out-of-memory killer still strikes during peaks, try 30. If the server swaps during normal traffic, drop to 5 or 1, and look for the memory hog instead of tuning further. Our guide on finding which process is eating your VPS RAM shows how to track it down.

How to tell if swap is actually helping

Wait a day, then check two numbers:

free -h
vmstat 1 5

In free -h, a few tens of megabytes of used swap with free RAM still available is healthy. In vmstat, watch the si and so columns (swap in, swap out). Occasional small numbers during peaks are fine; large numbers that never stop mean the server is thrashing, and swap is hiding a real shortage.

If PHP-FPM or the database stops getting killed during busy hours, swap did its job. To cut memory use at the source, tuning PHP-FPM pm settings for a 2GB RAM VPS and tuning MariaDB 11.x for a low-memory VPS usually free more RAM than any swap setting can.

Swap vs upgrading RAM

Swap is a safety net, not a plan. A 1GB VPS with 1GB of swap still has 1GB of fast memory; the rest runs at disk speed. On a typical SSD-backed VPS, reading from swap is dozens of times slower than reading from RAM, so any workload that lives in swap feels it.

Upgrade RAM when swap use stays high during normal traffic, when pages load slowly even though the CPU is idle, or when you have already trimmed services and still run close to the limit. A jump from 1GB to 2GB costs a few dollars a month on most providers, so check official pricing before you decide. That upgrade will do more for response times than doubling the swap file.

Common mistakes to avoid

  • Skipping the fstab entry. Swap works until the first reboot, then quietly vanishes, and the next peak kills a process again.
  • Setting swappiness to 0 and expecting no swap. On modern kernels, 0 means “avoid swap unless RAM is nearly exhausted”, not “never swap”. The out-of-memory killer can still fire.
  • Making the swap file enormous. A 4GB swap file on a 1GB VPS lets a memory leak run for an hour instead of a minute, then fills the disk and takes the server down.
  • Putting the swap file on a slow disk. On network-attached storage, swap latency is worse than on local SSD. If your provider offers local SSD, the default location is fine; if not, keep the file small.

Removing a swap file you no longer need

If you upgrade RAM and want the disk space back, turn swap off, remove the file, and delete the fstab line:

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

Run free -h afterwards to confirm the Swap row reads 0B. To enable swap on vps again later, repeat the same five steps.

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.