Allocate swap memory to Ec2 linux instance or any other linux machine

Hi, Today we are going to learn how to allocate swap memory to any linux instance. This particularly comes handy for Amazon Linux Ec2 instances as at the time of launch, Ec2 instances do not have swap memory attached to them and we need to attach it manually. But first, we need to know some basics about Swap memories in Linux. If you want to jump to the steps to allocate a swap then click here

What is Swap in Linux?

If you ever heard of the virtual memory, then in case of linux machines you are looking at swap memory. Swap space is the dedicated space from your hard disks or SSDs that is used when you Physical RAM is full. If the operating system is in need of more memory resources and the its RAM is full, inactive pages in memory are moved to the swap space. By inactive pages we mean the content that is not needed on urgent basis or not in use.

What are the benefits of having swap memory?

We can say swap is good in those cases where you need to run a memory intensive program and specially at the time when the system is idle. For e.g. midnight jobs like report generation, system cleaning etc where you don’t need performance. Then, swap is a good candidate.

How much space is required for swap memory space?

As a general rule of thumb, swap memory should have at least the same space as RAM but, recommended space may differ depending upon your RAM size. See the table below:

Memory SizeRecommended Swap Size
less than 2GB2x the size of RAM
2 GB or moreRAM + 2GB for e.g. for 4 GB Ram, Recommended size is 4+2 = 6GB
SWAP recommended Size

Steps to create a swap file in Linux

Swap file is nothing but a special partition of your hard disk that can be created easily using terminal. Here are the steps involved:

  1. Open up the terminal as root or sudo user and use the dd command to create swapfile in filesystem. Following command will create a 8 GB swap file (128MB x 64 = 8 GB)
$ sudo dd if=/dev/zero of=/swapfile bs=128M count=64

2. Give the proper permissions to the swap file

$ sudo chmod 600 /swapfile

3. Now use the mkswap command to setup a Swap area

$ sudo mkswap /swapfile

4. Using swapon command, we can use the newly created space for immediate use

$ sudo swapon /swapfile

5. update the /etc/fstab file to use it at boot time only

$ sudo vi /etc/fstab

6. Add the following line at the end of fstab file. then, save and exit

/swapfile swap swap defaults 0 0

7. Verify the newly created swap

$ sudo swapon -s

Is the swap replacement for more RAM and what are the downsides of SWAP?

No, swap is not the replacement of RAM and yes, there are some downsides of the having swap memory. Swap memory is in the hard disk and because of that they are pretty slow. So Imagine you are running out of RAM and system pushes one of the program pages to swap. Now, when the program is access that moved page, there will be transfer of information from Hard disk to RAM and will be slow. It will hamper the performance of the program badly.

Leave a Reply

Exit mobile version