How to add swap to a VPS that keeps running out of memory

Swap is disk space the system uses as spare memory when the RAM fills up. It does not make the server faster: it exists so that, in a peak, the system slows down instead of killing processes. Many new VPS come with no swap at all, which is why the database or the site “vanish” suddenly: the system ran out of memory and chose the biggest program to shut down (see out of memory on a VPS: what the OOM killer is). The commands are yours, on your own server: support does not run them for you (see how far our support goes).

First, see whether you have any

Run free -h and swapon --show. If the “Swap” line of the first shows zero and the second prints nothing, there is no swap. On a KVM server like our VPS, swap is yours to configure.

Creating a swap file

1 Pick the size. In the examples below it is 2G, a common starting point on small servers; adjust it to the disk space you can spare. Check the free space first with df -h.
2 Create the file: sudo fallocate -l 2G /swapfile. If that fails, the alternative is sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 (2048 times 1 MB).
3 Lock the permissions: sudo chmod 600 /swapfile. Only root should be able to read it, because swap can hold pieces of what was in memory, passwords included.
4 Prepare and switch it on: sudo mkswap /swapfile and sudo swapon /swapfile. Confirm with swapon --show and free -h.
5 Make it survive a reboot. First a copy: sudo cp /etc/fstab /etc/fstab.bak. Then add the line /swapfile none swap sw 0 0 to the end of /etc/fstab (for example with sudo nano /etc/fstab). A mistake in fstab can stop the server booting.

Tuning and undoing

I want to… Command
See how readily the system uses swap cat /proc/sys/vm/swappiness (the lower, the later it uses it)
Lower the value until the next reboot sudo sysctl vm.swappiness=10
Switch swap off sudo swapoff /swapfile, remove the line from /etc/fstab and delete with sudo rm /swapfile
Swap is not real memory. A disk is far slower than RAM: if the server uses swap all the time, the site gets slow with no error showing. Then look at what is eating the memory, and whether the application needs to be lighter or the plan needs more RAM. See how to see what is using CPU, memory and disk on your VPS. Each plan’s specifications are on the VPS servers page.
Measure before and after. Run free -h in a peak before creating the swap, and again after. If the swap’s “used” stays near zero, the server was just at its limit; if it climbs and stays high, the cause is elsewhere. Other common problems are in common VPS and Cloud errors.

Does the server still run out of memory? Tell us the plan and what runs on it.

Open a support ticket

SEE ALSO

Out of memory on a VPS: what the OOM killer is and what to do

How to see what is using CPU, memory and disk on your VPS

Common VPS and Cloud errors: SSH, memory, disk and reboots

VPS servers and their plans

RECOMMENDED PRODUCT

VPS server with root access

Resources of your own, the OS you choose, reinstall whenever you like. from 456,30 MT/mo (3-year plan, with coupon)

See plans
  • 0 Users Found This Useful
Was this answer helpful?