How to: Manage swap space

Published on 2015-12-31

A default cloud server at both Digital Ocean and Rackspace does not have any swap space allocated. This might cause your applications to fail when you run out of physical memory on the machine.

Fortunately Digital Ocean has an article about how to enable swap in Ubuntu 14.04 - which is a good place to start.

Basically these are the commands needed:

sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

and then add the following line to the /etc/fstab file:

/swapfile   none    swap    sw    0   0

As the article linked also points out there is a bit more to consider when using swap - but this is the basic setup.

You also need to consider the size of your virtual server if you constantly see it actually use the swap space - but for those rare once-in-a-while occations where the load on memory is a bit higher than usual it is very nice to have it ready to prevent a crash.