Have you every worried about your EC2 instance running out of memory, especially when you’re running a EC2 Micro instance witch only comes with a total of 613MB of memory. Well If you’re planning on running anything other then a basic web page you may run out of RAM can cause all sorts of problems, mainly it will cause your instance to crash. Well there is a tried an tested method to get around this called paging or swap space.
Paging works by creating an area on your hard drive and using it for extra memory, this memory is much slower than normal memory however this is much more of it available. The nice thing about using swap with your EC2 Instance is you have control of how much you get, which is especially nice because with some instance like the Micro you don’t get much memeory at all.
So how do we go about adding some swap space to our instance. Its pitty easy all you need to do is type the following into your terminal to create 1Gb of swap storage for your instance:
# sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024 # sudo /sbin/mkswap /var/swap.1 # sudo /sbin/swapon /var/swap.1
If you need more then change the “count=1024″ value to something higher. So what’s this all mean, lets break down each of these stages:
sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
Sudo – Root privileges are required for all thease steams
if=/dev/zero – Here we have the “if” variable or infile, this is the file that dd will be reading from, in this case it will only be coping across zeros as required for swap to start.
of=/var/swap1 – “of” or outfile is where dd will copy the data to, in this case we are creating a file called swap1 in the /var folder
bs=1M – This is the block size we want in our final file, in this case we have selected 1 megabyte
count=1024 – This is how many times we want to copy the block, we do this here to save on resources
sudo /sbin/mkswap /var/swap.1
This formats the file we created earler (var/swap.1) into the swap file format
sudo /sbin/swapon /var/swap.1
This actually turns the swap space on allowing the system to use the new swap space.
Turn Swap Off With “sudo /sbin/swapoff”
This command isn’t in the original command set because this disables the swap area.