Swap on a Low-Memory VPS: When It Helps and How to Set It Up
Creating a swap file in a few commands, tuning swappiness, the zram option, memory limits for containers, and the signs that you need more RAM rather than more swap.

Table of contents
On 1 or 2 GB VPS plans, running out of memory usually happens quietly: a Docker build or a heavy query wakes the kernel’s OOM killer and the database, or even dockerd itself, gets killed. Swap doesn’t replace RAM, but it is a cheap and effective cushion for exactly these short spikes.
Check the current state
free -h
swapon --show
cat /proc/sys/vm/swappiness
If swapon --show prints nothing, the server has no swap; many cloud images are deliberately shipped without it.
Creating a swap file
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
chmod 600is essential: swap can contain fragments of program memory, including passwords and keys.- If
fallocateisn’t supported on your filesystem, orswaponcomplains that the file “has holes”, create it withddinstead:sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress - Btrfs is different: the file has to be created with
btrfs filesystem mkswapfile.
After editing fstab, check that the new line is read correctly before rebooting: sudo swapoff /swapfile && sudo swapon -a.
How big?
| Server RAM | Suggested swap |
|---|---|
| 1 GB | 1 to 2 GB |
| 2 to 4 GB | 2 GB |
| 8 GB or more | 2 to 4 GB, mostly as insurance |
Servers don’t hibernate, so the old “twice your RAM” rule doesn’t apply. The goal is to absorb spikes, not to run from disk permanently.
swappiness: how eagerly to swap
The default vm.swappiness on most distributions is 60. On servers where latency matters (databases, web servers) a lower value tells the kernel to keep active program pages in RAM as long as possible and save swap for emergencies:
sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
Setting swappiness to zero is not recommended: on modern kernels it means “don’t swap until the last moment”, which raises the chance of hitting the OOM killer and largely defeats the purpose of the cushion.
zram: compressed swap inside RAM
zram creates a compressed virtual disk in memory. Pages that go to swap are compressed and stay in RAM; on VPSes with slow disks or IOPS limits it is usually faster than a swap file on disk. On Ubuntu and Debian:
sudo apt install zram-tools
Its settings live in /etc/default/zramswap:
ALGO=zstd
PERCENT=50
sudo systemctl restart zramswap
swapon --show
You can use zram and a swap file together. zram has the higher priority and is used first, with the swap file as a second reserve; the PRIO column in swapon --show shows the order.
Containers and memory
Swap doesn’t fix a container that eats memory without limit; it just delays its death and slows the whole server down in the meantime. Give your Docker services memory limits so one leaking service can’t take the others down:
services:
app:
image: myapp:latest
mem_limit: 512m
memswap_limit: 1g # total RAM + swap for this container
docker stats --no-stream shows each container’s real usage against its limit; more tools are in monitoring Docker containers.
When swap isn’t enough
Swap is for spikes, not for permanent work. Look at the si and so columns (pages swapped in and out) in vmstat: if they are constantly non-zero even under normal load, the server genuinely lacks RAM and adding more swap will only make it slower.
vmstat 1 10
dmesg -T | grep -iE "out of memory|killed process"
ps aux --sort=-%mem | head
If the logs show the OOM killer at work, first find which process holds the most memory. Sometimes a simple setting, such as a smaller innodb_buffer_pool_size in MariaDB, is cheaper than upgrading the server. And if you do decide to upgrade, see our criteria for choosing a VPS.
swapon failed: Operation not permitted. That limitation comes from the provider and only a KVM server solves it.


Comments
No comments yet. For technical questions, include the details (OS version, the exact error and the command you ran) to get a precise answer.