Back to Blog Systems
2 min read ✉ Subscribe
Swap is not optional on a small server
Systems

Swap is not optional on a small server

Without swap, the first allocation that cannot be satisfied is immediately fatal to something — and the kernel picks its victim by size, not by blame.

AX
Alien XP
Jul 28, 2026 · 2 min read

A lot of VPS images ship with no swap, and a lot of guides tell you that swap is obsolete on a machine with fast storage and plenty of RAM. Both of those are fine right up until a process asks for memory that is not there.

What actually happens without it

With swap, a memory spike is a slowdown. Cold pages move to disk, everything gets sluggish, your monitoring notices, and you have time to look. It is unpleasant and it is survivable.

Without swap, there is no intermediate state. The kernel cannot free anything, so it invokes the OOM killer, which scores every process and kills the one with the worst score — and score is dominated by size. That is almost never the process that caused the problem. It is your database, or your web server, because those are the biggest things running.

$ dmesg -T | grep -i 'killed process'
[Sat Aug  1 23:37:10 2026] Out of memory: Killed process 4135150 (php)

How much

Swap is insurance, not a second tier of memory. A huge swapfile does not help — it just makes thrashing last longer before something finally dies. Matching RAM up to about 2 GB and stopping there is a sensible rule for a small server.

fallocate -l 2G /swapfile || dd if=/dev/zero of=/swapfile bs=1M count=2048
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap sw 0 0' >> /etc/fstab
fallocate can leave a sparse file on some filesystems, and swapon refuses sparse swapfiles. If it fails, fall back to dd, which always writes a real extent.

Then tune when it gets used

# /etc/sysctl.d/60-vm.conf
vm.swappiness = 10
vm.vfs_cache_pressure = 150

The default swappiness of 60 will page out warm anonymous memory — your database's buffer pool, PHP's opcache — while there is still free RAM. That is the worst of both worlds: slower and still no headroom. At 10, the kernel only reaches for swap under real pressure.

Protect the things you cannot afford to lose

Swap buys you time, but you can also tell the kernel which processes to spare. For a database, a systemd drop-in is enough:

# /etc/systemd/system/mariadb.service.d/override.conf
[Service]
MemoryAccounting=yes
MemoryHigh=45%
OOMScoreAdjust=-500

MemoryHigh is a throttle rather than a limit — past that point the kernel applies reclaim pressure instead of killing anything. Deliberately not MemoryMax, which is enforced by killing the process inside the cgroup. A database killed to save memory has not saved you anything.

Filed under #linux#memory#oom#vps
AX

Alien XP

Systems · 10 essays

Runs the servers behind AlphaPanel and writes up what breaks.

Keep reading

All posts