Memory is tight and a build dies with Killed. See how to add a swap file, keep it after a reboot and when it will not save you.
The server works, but while installing packages or building a project a process suddenly dies with one short word: Killed. Most often it means memory ran out and there is no cushion in the form of a swap file. Here is how to add one in a few commands, how to make it survive a reboot and when it will not help.
In short. Create a file (
fallocate -l 512M /swapdemo), lock its permissions (chmod 600), format it (mkswap) and enable it (swapon). To enable it after a reboot, add a line to/etc/fstab. Check withswapon --showandfree -h. Swap is a cushion for a peak, not a replacement for memory: if memory is short all the time, you need a bigger plan.
First look at memory. The free -h command prints sizes in readable units, and the Swap row shows swap. On our server there was none:
Zeros in the Swap row mean there is no swap. The swapon --show command confirms it: when there is no swap it prints nothing. Note available: a little over 1 GB out of 3.8 GB, so the margin is small, and at a load peak processes will start to die.
Let us take 512 MB as an example. The fallocate command instantly reserves disk space for a file of the needed size. The file name is up to you; we called it swapdemo, in real life /swapfile is the usual choice:
sudo fallocate -l 512M /swapdemoA swap file holds pieces of memory, including passwords and keys, so only root should be able to read it. Lock the permissions:
sudo chmod 600 /swapdemoThe permissions -rw------- mean that only the owner can read and write. Now format the file as swap space:
The command prints the size and the identifier itself and there should be no errors. If the file system does not support fallocate for such files, use dd instead: sudo dd if=/dev/zero of=/swapdemo bs=1M count=512.
sudo swapon /swapdemoThe command prints nothing if everything worked. Let us check:
The Swap row now shows 511 MB with zero used: the file is attached, but the kernel does not touch it while memory is enough. That is the normal state of a cushion.
After a reboot the file will not come back on its own. To make the system attach it at start, add this line to the /etc/fstab file:
/swapdemo none swap sw 0 0In our example we did not add the line and removed the file after the demo, so use the real name of your own file. After editing fstab, check with sudo swapon --show and reboot at a convenient time.
For a small VPS 1-2 GB is usually enough, and there is no point in taking more than the amount of RAM. Swap lies on disk, so it is slower than memory: it saves you from a crash, but does not make the server faster. The vm.swappiness parameter sets how eagerly the kernel uses swap. The default is 60, as sysctl vm.swappiness shows. To make the kernel touch swap less, you can set sudo sysctl vm.swappiness=10 for now, and for a permanent value add a line to a file in the /etc/sysctl.d/ directory.
Two typical cases. If swapon warns about insecure file permissions, you skipped chmod 600: run it and repeat the command. If you get an error such as "read swap header failed", the file has not been formatted: run mkswap and enable it again. Check the real message text before you fix anything, as it can differ slightly between system versions.
While the server has enough memory, the used part of swap stays close to zero. If it grows, look at the statistics: vmstat 1 3 prints them three times, one second apart. The si and so columns show how much data went into swap and came back. A few non-zero values are harmless, constant ones are a sign that memory is short and a bigger plan is due.
If the server uses swap all the time while the processor waits for the disk, it "spins its wheels": pages race between memory and disk. A bigger file does not fix that, a plan with more memory or an optimised application does. The reasons processes get killed are covered in the article on the OOM killer and load average, and the general order of hunting a bottleneck is in diagnosing a slow server.
First disable it, then delete it. One command does both:
sudo swapoff /swapdemo && sudo rm /swapdemoAfter that swapon --show prints nothing again, and the line in /etc/fstab must be removed if you added it.
Create a file with fallocate, lock the permissions with chmod 600, format it with mkswap and enable it with swapon. For autostart add a line to /etc/fstab.
Usually 1-2 GB for a small server, no more than the amount of RAM. More makes no sense: swap is slower than memory.
You can if you have plenty of memory. But at peaks processes will die with Killed, and a small cushion often prevents that.
With swapon --show or by the Swap row in the free -h output.
Swap row in free -h and an empty swapon --show mean there is no swap.fallocate, chmod 600, mkswap, swapon./etc/fstab.