How to add Swap File on Linux Cloud Server

It is common that swap partition was created and reserved during installation of on-premise Linux server.  Even if it was not, you can always add swap partition easily as long as you still have spare disk space. That is not the case if you rent Linux cloud server.  The cloud server is provisioned with Linux OS and its applications so that you don’t have to go through the manual installation process like you do with your on-premise server. However by default a swap partition is not present and you can’t add or alter the partition scheme on the cloud server.

If you rent a small Linux cloud server with only 256 MB of RAM, most likely you will experience ‘out of memory’ crashing problem even when running just one web application. That was the problem that I got until I read some articles and realize that Linux swap space doesn’t have to be placed on a dedicated disk partition.  It can be created as disk file, pretty much like Windows swap file.

Swap space is incredibly useful in avoiding some of these ‘out of memory’ common problems.  Of course, the best solution is to upgrade your cloud server. Adding swap space, however, can give you more flexibility and can help buy you time on a less powerful server.

Here are steps to add swap file on Ubuntu cloud server that will give you some breathing room in terms of your RAM usage.

Check for Swap Space:

You can check whether swap space is currently present on your cloud server by entering following command:

sudo swapon -s

Alternatively you can also check with this utility:

free -m

Check Disk Space:

If  it is confirmed that there is no swap space on your server and you need to create a swap file, you need to check how much disk space is available with the following command:

df -h

Although there are many opinions about the appropriate size of a swap space, generally, an amount equal to or double the amount of RAM on your server is a good starting point. So if your RAM size is 256 MB then you will need 512 MB of the current free disk space for the swap file.

Create and Activate Swap File:

To create and activate 512 MB of swap to your server, follow these steps:

  1. Create the file to be used for swap.
       sudo fallocate -l 512m /swapfile
       sudo chmod 600 /swapfile
  2. If fallocate fails or is not installed, run the following command.
       sudo dd if=/dev/zero of=/swapfile bs=1024 count=524288
       sudo chmod 600 /swapfile
  3. Format the file for swap.
       sudo mkswap /swapfile
  4. Add the file to the system as a swap file.
       sudo swapon /swapfile

Now that you have activated the swap file, check and verify swap space using either ‘sudo swapon -s’ or ‘free -m’ command.

Make Swap Space Permanent:

Swap space will last only until you restart your server. Once your server is rebooted, the swap space is deleted and you need to enable it manually again. So to make the swap space permanent, add this line to the end of /etc/fstab:

/swapfile  none  swap  sw  0  0

Enable Swappiness:

The swappiness parameter configures how often your server swaps data out of RAM to the swap space. This is a value between 0 and 100 that represents a percentage.

How much swappiness affects performance depends on how your memory is being used, so experiment to find an optimal value. At 0 the swap file will only be used when the system runs completely out of memory. Higher values let the system swap idle processes out to allow the system to free memory for disk caching, potentially improving overall system performance.

For a Desktop, a swappiness setting of 60 is not a bad value. For a Server, you’d probably want to move it closer to 0.

You can see the current swappiness value with the following command:

cat /proc/sys/vm/swappiness

To change the swappiness value permanently edit /etc/sysctl.conf and add or alter the following line.

vm.swappiness=10

Start with a value of 10 and increase if needed.

Reboot

Finally, reboot the server to ensure that all the changes go into effect.