Amazon EC2: Adding Swap

So, after upgrading to R 3.2.0 on my EC2 instance, I was installing newer versions of various packages and I ran into a problem with dplyr: virtual memory exhausted!

Seemed like a good time to add some swap.

Adding Swap and Turning it On

First make some swap files. I am in favour of creating a few smaller swap files rather than a single monolithic one.

sudo dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
sudo dd if=/dev/zero of=/var/swap.2 bs=1M count=1024
sudo dd if=/dev/zero of=/var/swap.3 bs=1M count=1024

Another way to create swap files is by using fallocate, which actually provides a more intuitive interface.

sudo fallocate -l 1G /var/swap.1

To make sure that these files are secure, change the access permissions.

sudo chmod 600 /var/swap.[123]

Next you’ll set up a swap area on each of these files.

sudo /sbin/mkswap /var/swap.1
sudo /sbin/mkswap /var/swap.2
sudo /sbin/mkswap /var/swap.3

Finally activate as many of the swap files as you require to give you sufficient virtual memory. I just needed one for starters.

sudo /sbin/swapon /var/swap.1

If you want the swap space to be activated again after reboot then you will need to add an entry to /etc/fstab. More information can be found here.

Turning it Off Again

When you are done with the memory intensive operations you might want to disable the swap files.

sudo /sbin/swapoff /var/swap.1

Here’s everything in a Gist (using just a single swap file and setting its size from an environment variable).