On Amazon EC2, the instance type you choose determines the amount of physical memory (RAM) that you get. Larger instance types with lots of RAM are more expensive. So if you want more RAM on a medium or small EC2 instance, you can define some storage space on your disk to act as RAM when needed. The disk area you use for this purpose is called a swap file. This article explains swap files and demonstrates how to set up a 2GB swap file using ephemeral storage on an m3.medium instance of Amazon EC2 that has only 3.75GB of RAM by default.
The how to section of this article applies to Ubuntu EBS-backed Amazon EC2 instances but it should also work for any debian-based distro as well. If your set up is significantly different from mine (not EBS backed for example), you can still use this article as a guide but you’d have to figure out the details yourself.
UPDATE: While this article focuses on adding a swap file to Amazon EC2 instances that come with instance/ephemeral storage, I have written another article that discusses how to add a swap file to Amazon EC2 instances that are “EBS Only” (without instance/ephemeral storage). Read it here: Adding A Swap File To An “EBS Only” Amazon EC2 Instance
Swap Spaces And Swap Files
Swap space gets used when your system needs physical memory for active processes and there is not enough unused physical memory available. It is disk space used in Linux (and other operating systems) to store memory objects when actual RAM starts becoming crowded. Since disk is slower than memory, Linux first stores in swap the least recently used objects, and keeps as much as of it as possible in memory. If possible, the general recommendation is to have a swap space equal in size to your memory.
Since the access time for swap is slower, it is not meant to be a complete replacement for the actual RAM. Swap space can be a swap file or a dedicated swap partition, or a combination of both. This article discusses swap files.
Why You Need A Swap File On Amazon EC2
Amazon EC2 instances never seem to come pre-configured with swap files (at least the ones I’ve played with didn’t). You can easily check if your instance has a swap file by using any of these three methods:
- free command
- top command (there’s a line for swap before the list of processes)
- swapon -s command
For example, here’s a screen shot of the top command output with the swap file line highlighted in yellow.
An m3.medium instance of EC2 comes with 3.75GB of physical memory. While this might be sufficient for a lot of stuff, if your web application is reasonably sized, that memory could get used up pretty fast and you might start seeing errors in processes like MySQL and others. From my experience many of such memory related errors can be fixed by the addition of a swap file.
Where Should The Swap File Be Set Up?
EBS backed EC2 instances have their root volume on EBS (Elastic Block Store) and they also have another type of storage called “instance storage” or “ephemeral storage” that is physically attached to the machine. Data stored on ephemeral storage is not persistent (it disappears when the instance is stopped but not when it is rebooted). Now you have two options for where to create your swap file: Either directly on EBS or on ephemeral storage.
A major deciding factor is that EBS is not free (you pay for I/O operations) but ephemeral storage is always free. And since RAM is not persistent as well, ephemeral storage has sort of become the default place for swap files. It’s as if swap files and ephemeral storage have been made for each other.
Ephemeral means “lasting for a very short time”.
There are some situations, however, where you would want to (or have to) set up your swap file on an EBS volume. For example, t1.micro instances do not support ephemeral storage so you really have no choice but to use EBS in that situation. Here, I’ll be setting up a swap file on ephemeral storage but you can easily adapt the instructions to work for EBS.
Ephemeral storage is mounted at /mnt after startup.
How To Set Up The Swap File
Before you start creating the swap file, confirm that you don’t already have swap space allocated using the top or free or swapon -s commands mentioned previously. You should also confirm that you have an EBS backed EC2 instance with ephemeral/instance storage mounted at /mnt (you can check this with the lsblk command).
We will be setting up a 2GB swap file for an m3.medium instance with 3.75GB of RAM. If you’re curious about how much swap file you need to create in your case, you can have a look at this link.
Now run this command:
sudo dd if=/dev/zero of=/mnt/swapfile bs=1M count=2048
That command will write 2GB worth of zeroes to the new file called “swapfile” on /mnt. Now set the permissions for “swapfile” with these two commands:
sudo chown root:root /mnt/swapfile sudo chmod 600 /mnt/swapfile
Then run these two commands:
sudo mkswap /mnt/swapfile sudo swapon /mnt/swapfile
The mkswap command converts the “swapfile” full of zeroes into an actual Linux swap file (swap space). The swapon command sets up “swapfile” as the system swap file.
In order to make sure that this swap space is mounted after a system reboot, you need to update the fstab file (located in /etc/fstab). Append this line using your preferred text editor:
/mnt/swapfile swap swap defaults 0 0
Now enable swapping with:
To confirm that the new swap file is in use, run the swapon -s command. To check the ammount of swap space, run either the top or the free commands.
NOTE: This swap space will not survive when the instance is stopped and later started because the ephemeral storage where the swap file is stored gets destroyed when the instance stops. However, the swap file will survive a reboot. If you must do a stop/start, remember to recreate the swap file using the instructions here with the exception of the fstab changes since those are stored in EBS.