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 become root and then make some swap files. I am in favour of creating a few smaller swap files rather than a single monolithic one.

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

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

# chmod 600 /var/swap.[123]

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

# /sbin/mkswap /var/swap.1
# /sbin/mkswap /var/swap.2
# /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.

# /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.

# /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).

Categorically Variable