How to Create a SWAP space in an AWS Micro Instance


If you are using a Micro Instance in AWS then you can find there is no swap space with it. But, it always requires a swap space if you are running applications that require a large amount of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available. As per the old rule the swap partition should be twice the size of the physical memory. As per Red Hat Recommendation- “System with 4GB of ram or less require a minimum of 2GB of swap space”. In this article I will show you how to find out your swap space and how to create it.

First we will determine that how much swap space we have.

To check the free available physical memory as well as swap memory

[root@ip-10-0-1-41 admin] # free -m

To check the disk space & partition layout

[root@ip-10-0-1-41 admin] # df -h

You can verify the swap space using the below commands:

[root@ip-10-0-1-41 admin] # swapon -s


[root@ip-10-0-1-41 admin] # cat /proc/swaps

We can create swap space using two methods with dd, mkswap and swapon commands.

First Method :-

Create a swap file

[root@ip-10-0-1-41 admin] # dd if=/dev/zero of=/swap bs=1M count=1024

Make the file as a swap file

[root@ip-10-0-1-41 admin] # mkswap -f /swap

Enable the newly created swap file

[root@ip-10-0-1-41 admin] # swapon /swap

Second Method :-

If you have additional free hard disk then you can use that as your swap partition. Assuming your free disk name as “/dev/sdf”

[root@ip-10-0-1-41 admin] # mkswap -f /dev/sdf

Enable the swap partition for usage

[root@ip-10-0-1-41 admin] # swapon /dev/sdf

To Make the swap space available after boot you have to edit the /etc/fstab file.

[root@ip-10-0-1-41 admin] # vi /etc/fstab

  • /swap                  swap                    swap    defaults        0 0


  • /dev/sdf               swap                    swap    defaults        0 0


Now you can verify the swap space again using the earlier discussed command.

I have added my EC2 Instance to my SAAS Monitoring tool. You can see the screenshot of the added Monitor to verify that the swap memory is also being monitored.



Don’t forget to like & share this post on social networks!!! I will keep on updating this blog. Please do follow!!!




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s