Using Swap Space (Virtual memory) with Bytemark Cloud Servers


By default, we don’t have swap space (ie: virtual memory) enabled on our Cloud Servers as it can be fairly detrimental if overused, as swap is usually a lot slower than normal RAM, and due to the disks not being directly attached to the virtual machine host (which is what allows us to live migrate servers and other nice things) there’s a little more latency, which makes it appear much slower.

However, its fairly simple to enable swap and configure it so it’s only used as a last resort rather than normal day-to-day swap, lightening the load on the physical RAM and providing a buffer in the event the server runs out of memory, without affecting normal performance.

I’d typically suggest limiting it to 1GB of swap, but you can increase this if specifically needed, but avoid setting it too high.

Setting up swap space on Cloud Servers

To configure and enable it, you’ll need to log into the server either as a user with sudo access, or the root user itself.

First you will need to either create a new swap file and set the permissions so only the root user can access it. The 1048576 below equates to 1GB, but you can multiply it by other numbers to get 2, 4 or an arbitrary size:

sudo dd if=/dev/zero of=/swap.file bs=1024 count=1048576
sudo chmod 600 /swap.file

Next, you should ensure that the swap file only gets used when necessary using the “swappiness” kernel parameter, which will avoid swap being used when there’s normal RAM available:

echo 1 | sudo tee /proc/sys/vm/swappiness
echo "vm.swappiness = 1" | sudo tee -a /etc/sysctl.conf

Next, turn that file into a swap file, and enable it:

sudo mkswap /swap.file
sudo swapon /swap.file

At this point, if you use free, it should report having swap configured!

Finally, you will want to set it to be used when the server is next booted, either using (be very careful to ensure you have the -a in the second part!):

echo "/swap.file  swap  swap  auto  0  0" | sudo tee -a /etc/fstab

Or, edit /etc/fstab manually and add the text:

/swap.file  swap  swap  auto  0  0

And you should be done, with 1GB of swap (assuming you didn’t change the number in the sudo dd line) which will be enabled the next time you start the server.

Disabling/Enabling swap

If later on you decide you no longer want or need swap enabled, then you can turn it off temporarily with:

sudo swapoff

or turn it back on temporarily with:

sudo swapon /swap.file

This will revert once the server is rebooted however, so turn it on or off permanently you will need to edit /etc/fstab and remove (or comment out) or add the line:

/swap.file  swap  swap  auto  0  0

As always, if you have any questions or run into any problems you can either post them below or send them to



you can’t pipe to a file with sudo – these lines will fail:

sudo echo 1 > /proc/sys/vm/swappiness
sudo echo "vm.swappiness = 1" >> /etc/sysctl.conf

sudo echo "/swap.file  swap swap auto 0  0" >> /etc/fstab

I’d recommend using tee to overwrite and tee -a to append.

echo 1 | sudo tee /proc/sys/vm/swappiness
echo "vm.swappiness = 1" | sudo tee -a /etc/sysctl.conf

echo "/swap.file  swap swap auto 0  0" | sudo tee -a /etc/fstab


Thanks @telyn, post fixed!