Tips28 Aug 2026Joy Team2 min read

How to add swap space on Ubuntu 24.04 to prevent "Out of Memory" crashes

Essential for 1 GB / 2 GB VPS users: create a swap file on Ubuntu 24.04 to prevent OOM errors and keep MySQL running smoothly on Joy Cloud.

Small servers run out of memory at the worst moment — an apt upgrade, a Composer install, a traffic spike — and the kernel kills the biggest process, which is usually MySQL. A swap file gives the kernel somewhere to put idle pages so that the process survives. NVMe makes swap far less painful than it used to be.

1. Check

free -h
swapon --show   # empty output = no swap

2. Create a swap file

A good size is 1–2× RAM for servers up to 2 GB, and 2 GB flat above that.

fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap sw 0 0' >> /etc/fstab

3. Tune swappiness

By default the kernel starts swapping early. On a server you usually want it to prefer RAM:

sysctl vm.swappiness=10
echo 'vm.swappiness=10' >> /etc/sysctl.d/99-swap.conf

4. Verify

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

When swap is not the answer

If free -h shows swap in constant use and the load average climbs, the server is simply too small: upgrade to the next plan from the server's Billing tab (applied live, pro-rated) rather than swapping to NVMe all day. Check MySQL's innodb_buffer_pool_size too — the default on small servers is often larger than the RAM can spare.

ubuntuswapmysql
Was this article helpful?
Corrections and suggestions go straight to the team that wrote it.