Swap Space On EC2
Recently, while investigating some mysterious crashes we were experiencing, I discovered that certain EC2 instance types have no allocated swap space. This post covers some basics of swap, what I learned about swap partitions on EC2 instances, and what you can to create swap on the machines that do not have any.
What is swap?
Swap space is an area on disk that temporarily holds a process memory image. When physical memory demand is sufficiently low, process memory images are brought back into physical memory from the swap area on disk. Having sufficient swap space enables the system to keep some physical memory free at all times.
There are three types of swap: device swap, file system swap and pseudo swap. Device swap is a separate partition allocated when the disk is configured. File system swap allows you to use the existing file system to allocate swap space. Pseudo swap is allocated from system memory on some systems. More on the different types of swap here
So some machines don't have any?
Using the linux free command, I found that our Small and Medium instances have an 896MB swap partition (device swap), but Large and Extra-Large instances do not have a swap partition -- nor can one be created.
How can I create swap on these machines?
Though we cannot create device swap, we can still create create file system swap. Here's a script (thanks to RightScale) that we can use to create a swap file on these larger instances:
#!/bin/bash if [ $SWAP_SIZE_MEGABYTES -eq 0 ];then echo No swap size given, skipping. else if [ -e /swapfile ];then echo /swapfile already exists. Skipping. else echo Creating /swapfile of $SWAP_SIZE_MEGABYTES Megabytes dd if=/dev/zero of=/swapfile bs=1024 count=$(($SWAP_SIZE_MEGABYTES*1024)) mkswap /swapfile swapon /swapfile echo Swap Status: swapon -s fi fi