TürkiyeVPS

How to Add Swap on Ubuntu 24.04: Low-RAM VPS Guide

If apps keep dying on your 512 MB or 1 GB VPS, swap is the first aid. Learn how to add swap on Ubuntu 24.04, pick the right size and understand where swap stops helping.

7 min read

You got a small VPS, installed a few services, and one day MySQL or your Node.js app simply disappeared. Most of the time the reason is boring: the server ran out of RAM and the Linux kernel killed a process to free memory. This guide shows how to add swap on Ubuntu 24.04, how to size it, how to make it permanent, when zram is the better choice, and when swap is no longer the answer. It's written with 512 MB and 1 GB RAM servers in mind.

What swap is — and what it isn't

Swap is disk space the kernel uses to park rarely used memory pages when RAM fills up. It can be a file (swap file) or a dedicated partition. On a VPS, a swap file is by far the most practical option.

What swap does well:

  • When memory spikes, the system slows down instead of killing your processes.
  • Memory that was loaded at startup and is rarely touched gets moved to disk, leaving RAM for real work.
  • Short but memory-hungry jobs such as apt upgrade, npm install or composer install stop failing halfway.

But swap is not a substitute for RAM. Even the fastest SSD is far slower than memory, and a server that swaps constantly feels sluggish everywhere. Think of swap as a seat belt, not an extra engine.

1. Check the current state

Look at memory and swap first:

free -h
swapon --show

If swapon --show prints nothing, there's no swap. If it lists something, swap already exists — review its size before adding more.

Then check disk space:

df -h /

On small plans this matters. An entry-level VPS may have only 5 GB of disk, and a 2 GB swap file would eat a big chunk of it. On such servers, 512 MB to 1 GB of swap is usually a sensible balance.

2. Choose a swap size

There's no single formula, but this is a good starting point for small servers:

RAM Suggested swap Note
512 MB 512 MB – 1 GB 512 MB if disk is tight
1 GB 1 GB Balanced for most small projects
2 GB 1 GB – 2 GB Buffer for spikes
4 GB+ 1 GB – 2 GB Mostly a safety net

Servers don't hibernate, so old rules like "twice the RAM" don't apply.

3. Create the swap file

The example below creates 1 GB of swap. For 512 MB, use 512M instead of 1G.

sudo fallocate -l 1G /swapfile

On some filesystems fallocate fails. In that case create the same file with dd (a bit slower):

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

Restrict permissions to root, format the file as swap and enable it:

sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Verify:

swapon --show
free -h

If the Swap: line shows the new space, you're done with the basic part.

"Operation not permitted" or "Invalid argument"

If swapon returns one of these errors, the virtualization layer underneath your server may not allow swap files. Remove the file (sudo rm /swapfile) and ask us via live chat on the site or a support ticket in the customer panel. Meanwhile, the RAM-saving tips further down can help.

4. Make swap permanent

Swap enabled with swapon disappears after a reboot. To keep it, add it to /etc/fstab — and back the file up first:

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

Check the result:

tail -n 3 /etc/fstab

A broken fstab can stop the server from booting properly, which is why the backup matters. If something goes wrong, restore it with sudo cp /etc/fstab.bak /etc/fstab from the web terminal in the customer panel.

5. Tune swappiness and cache pressure

vm.swappiness controls how eagerly the kernel swaps (0–200; Ubuntu's default is 60). On servers, a lower value keeps applications in RAM and lets swap kick in only when it's really needed.

cat /proc/sys/vm/swappiness

Make the setting persistent:

sudo tee /etc/sysctl.d/99-swap.conf > /dev/null <<'EOF'
vm.swappiness=10
vm.vfs_cache_pressure=50
EOF
sudo sysctl --system

vm.vfs_cache_pressure=50 keeps filesystem metadata (directory and inode caches) in RAM a little longer. These values are a reasonable starting point for small servers; adjust them by testing your own workload.

Alternative: compressed swap with zram

zram turns part of your RAM into a compressed swap device. Because it lives in memory, it's much faster than a swap file and uses no disk space — at the cost of some CPU time. It's a great fit for small VPS plans with limited disk.

sudo apt install -y zram-tools
sudo nano /etc/default/zramswap

Set these values (the default algorithm is lz4; zstd uses a bit more CPU but compresses better):

ALGO=zstd
PERCENT=50
PRIORITY=100

PERCENT=50 sizes the zram device at half of your RAM (measured as uncompressed data). PRIORITY=100 makes the kernel use zram before any disk-based swap. Restart the service:

sudo systemctl restart zramswap
swapon --show

If /dev/zram0 appears, zram is active. You can run zram and a swap file together; thanks to the priority, zram fills up first. If the zram module can't be loaded on your server, contact support.

How to remove swap

sudo swapoff /swapfile
sudo sed -i '\|^/swapfile none swap sw 0 0$|d' /etc/fstab
sudo rm /swapfile

swapoff moves swapped pages back into RAM, so it can take a while if memory is tight.

How to tell you're out of memory (OOM)

When memory is completely exhausted, the Linux OOM killer terminates a process. You'll find the evidence in the kernel log:

sudo journalctl -k | grep -i -E "out of memory|oom"
sudo dmesg -T | grep -i -E "killed process|out of memory"

To see what's using RAM right now, run top (then press Shift+M) or ps aux --sort=-%mem | head. More commands like these are in our server management commands guide.

Other ways to save RAM

  • Pick lighter software: Nginx instead of Apache; SQLite instead of MySQL where it's enough.
  • Shrink MySQL/MariaDB: keep innodb_buffer_pool_size low on small servers, e.g. 64M–128M.
  • Limit PHP-FPM: pm = ondemand with a low pm.max_children stops idle workers from hogging memory.
  • Cap Docker containers: a limit such as docker run --memory=256m ... prevents a single container from freezing the whole box. New to Docker? See our Docker on Ubuntu 24.04 guide.
  • Turn off unused services: list them with systemctl list-units --type=service --state=running.

When to upgrade RAM instead

Swap is for absorbing spikes. If free -h shows swap permanently full, the si and so columns in vmstat 5 stay above zero, or the server feels noticeably slow, what you really need is more RAM. At that point moving to a bigger plan beats any amount of swap tuning. We wrote honestly about what to expect from budget servers in what to check before buying a cheap VPS.

Frequently Asked Questions

Does swap wear out the SSD?

Swap does write to disk, but with a low swappiness and occasional use the extra wear is tiny. Constant heavy swapping is bad for performance anyway — that's your signal to add RAM.

What can I run on a 512 MB VPS with swap?

A simple bot, a small static site or a lightweight API can run fine on 512 MB of RAM plus a little swap. For heavier stacks such as a database, WordPress or Docker, 1–2 GB of RAM is a more comfortable start.

zram or a swap file?

If disk space is limited and your CPU is mostly idle, zram is usually faster and more practical. Using both, with zram at a higher priority, is also a solid setup.

When you need more memory

TürkiyeVPS VPS plans in Istanbul start at $0.89/month, with 1 GB and 2 GB RAM options available. If you want dedicated CPU cores and guaranteed RAM, our VDS plans start at $1.69/month. Every plan includes unlimited traffic and full root access. Want to grow an existing server? Just open a support ticket — we'll move you to a larger plan with your data intact.