How To Create Swap Area In Running EC2 Instance In AWS

In this blog, We are going to create swap area in running ec2 instance, But before we are going on AWS for creating swap area in ec2 instance.  We should have the answer of following question 

  • What Is Called Swap Area ?
  • What Is Ideal Size Of Swap Area ? 
  • How To See Swap Area On Running Instance ?

So, Lets see the answer of each question one by one in below section 

What Is Called Swap Area ?

                                    Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory.
                                   Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files.

What Is Ideal Size Of Swap Area ?

                                    Many years ago, the rule of thumb for the amount of swap space that should be allocated on the hard drive was 2X the amount of RAM installed in the computer (of course, that was when most computers’ RAM was measured in KB or MB). So if a computer had 64KB of RAM, a swap partition of 128KB would be an optimum size. This rule took into account the facts that RAM sizes were typically quite small at that time and that allocating more than 2X RAM for swap space did not improve performance. With more than twice RAM for swap, most systems spent more time thrashing than actually performing useful work.
                                      RAM has become an inexpensive commodity and most computers these days have amounts of RAM that extend into tens of gigabytes. Most of my newer computers have at least 8GB of RAM, one has 32GB, and my main workstation has 64GB. My older computers have from 4 to 8 GB of RAM.
                                       The following table provides the recommended size of a swap partition depending on the amount of RAM in your system .
Amount of RAMRecommended swap space
≤ 2GB

"2X RAM"
2GB – 8GB"= RAM"

How To See Swap Area On Running Instance ?

                                        You can also use the swapon -s command, or top, free, or any of several other commands to verify this.
[root@studentvm1 ~]# free
              total        used        free      shared  buff/cache   available
Mem:        4038808      382404     2754072        4152      902332     3404184
Swap:      10485756           0    10485756
[root@studentvm1 ~]#

Create Swap Area In Running EC2 Instance In AWS ?

  • Use the dd command to create a swap file on the root file system, where “bs” is the block size and “count” is the number of blocks. In this example the swap file is 4 GB
  • bs= sets the blocksize, for example bs=1G would be 1GiB blocksize.

  • count= copies only this number of blocks (the default is for dd to keep going forever or until the input runs out). Ideally blocks are of bs= size but there may be incomplete reads, so if you use count= in order to copy a specific amount of data (count*bs), you should also supply iflag=fullblock.

 $ sudo dd if=/dev/zero of=/swapfile bs=1G count=4
  •  Update the read and write permissions for the swap file
$ chmod 600 /swapfile
  •  Set up a Linux swap area
$ mkswap /swapfile
  •  Make the swap file available for immediate use by adding the swap file to swap space
$ swapon /swapfile
  •  Verify that the procedure was successful
$ swapon -s
  •  Enable the swap file at boot time by editing the /etc/fstab file
$ vi /etc/fstab
$ /swapfile swap swap defaults 0 0
  •   You can also use the swapon -s command, or top, free, or any of several other commands to verify this.
 $ free -h