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.
Another way to create swap files is by using
fallocate, which actually provides a more intuitive interface.
To make sure that these files are secure, change the access permissions.
Next you’ll set up a swap area on each of these files.
Finally activate as many of the swap files as you require to give you sufficient virtual memory. I just needed one for starters.
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.
Here’s everything in a Gist (using just a single swap file and setting its size from an environment variable).