Shurn the Awesomer
Swap Space in EC2 Ubuntu

Swap Space in EC2 Ubuntu

Written on Mon, 13 February 2017

Over the course of running this webserver and zimbra, I realised that there is a need to have swap space whenever you have applications that are memory hungry. My webserver has occasionally run out of memory on t2.nano instance and my zimbra webserver frequently has memory issues, almost always due to MySQL/MariaDB. MySQL/MariaDB certainly is memory hungry for some reasons, so I guess until I can figure out how to optimise these applications, I just have to work with either increasing RAM or having swap space.

The Ubuntu instances will likely not have swap space. You must configure one. In this tutorial, I will make things really easy by making a swapfile in the filesystem instead of creating a partition

Before you proceed with the tutorial, you must take into consideration of several matters. If you EBS volume is Magnetic, sc1, or st1, having a swap will reduce the performance of applications. Spinning disk will always be slower than RAM or SSD. However, if you are gp2 or io1, then you will see noticable performance of having swap on SSD than on HDD. But you will degrade the SSD fast due to many write cycles. Either way, having swap is a very good safeguard and comparatively affordable solution against application error or crashes related to out-of-memory situations.

Incompatibilities


You must also take note if your file system supports swap files. At the time of this writing, btrfs is not compatible with swap. You probably want to create a partition instead.

Step 1: Checking for existing Swap


We can see if the system has any configured swap by typing:

sudo swapon --show


If there is no output, that means you have no swap space currently.

You can verify no existing swap with:

free -h


If you see that swap has 0B, that means there is no active swap present in system.

Step 2: Check available space in file system


We check the available space in the filesystem with:

df -h

Take note of the space space available in /dev/xvda1. If you intend to have swap space on other EBS, then check the available space of it.

Planning your swap space


In a standard Ubuntu installation, you should have 8GB hard disk space, of which, the OS and the applications will consume some. You would probably be left with about 5GB. It is usually recommended to have twice as much swap as your RAM. Of course, you are free to have as much swap as you need. In a t2.nano instances, you will only have 512MB RAM. so I'm going to have 1GB of swap space.

Step 3: Creating Swap File


I'm going to create 1GB of swap space:

sudo fallocate -l 1G /swapfile

We can verify that the correct amount of space was reserved with:

ls -lh /swapfile


You should see something like:

-rw-r--r-- 1 root root 1.0G Feb 13 07:35 /swapfile

Step 4: Enabling swap space


For security reasons, we should only allow users with root privileges to read the contents. All other users should not have access to the swap file.

sudo chmod 600 /swapfile

Verify the permissions change with:

ls -lh /swapfile

You should see something like:

-rw------- 1 root root 1.0G Feb 13 07:35 /swapfile

We now mark the file as swap space with:

sudo mkswap /swapfile

You should see an output like:

Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=0d7257f1-9def-4542-bcba-34b8f50f2822

Now we enable swap:

sudo swapon /swapfile

Verify the swap is enabled with:

sudo swapon --show


You will now see a swap file with 1024M.

You can also check with:

free -h


You will see 1.0G of space for use.

Step 5: Making Swap Permanent


If you reboot your computer, the setting you just did will be gone. You can make swap a permanent feature during reboot.

Add swap file information to fstab with:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

That's it!

Step 6: Configuring swappiness (Optional)


Swappiness is a parameter for the system to determine how often your system swaps data out of RAM to swap space. The values are represented from 0 to 100, in percentages. By default, the value is 60.

You must remember that a swap on HDD is time costly, while a swap in SSD is lifespan costly. Since I'm running a server, I would prefer a value closer to 0, only to use RAM when absolutely neccessary. In some use cases, using swap actually increases performance.

Choose your ideal percentage, then run the following:

sudo sysctl vm.swappiness=10


I chose 10 so that it will use swap sparingly.

To make this value permanent during reboots, open /etc/sysctl.conf:

sudo nano /etc/sysctl.conf


Then add the following at the bottom:

vm.swappiness=10

Conclusion


Swap has many many advantages. In some cases, it will save a technician from certain doom due to application crashes.

Generally swap is the most affordable alternative to getting RAM upgrades, or even CPU upgrades. Use it wisely and you will have a well-oiled affordable machine that will last you very long.

If you have to create a swap partition instead of a swap file, check out my other tutorial on creating a swap partition.