How do I use a partition on my hard drive to allocate memory to work as swap space on an Amazon EC2 instance?

4 minute read

I want to use a partition on my hard drive to allocate memory to work as swap space on an Amazon Elastic Compute Cloud (Amazon EC2) instance.

Short description

To allocate memory as swap space, complete the following steps:

  1. Calculate the swap space size.
  2. Create a partition on your hard disk as swap space.
  3. Set up the swap area.

You can also create a swap file to use as a swap space. For more information, see How do I allocate memory to work as swap space in an Amazon EC2 instance by using a swap file?

Note: The instance uses swap space when the amount of RAM is full. You can use swap space for instances that have a small amount of RAM, but it's not a replacement for more RAM. Because swap space is located on the instance's hard drive, performance is slower when compared to actual RAM. For more or faster memory, increase your instance size.


Calculate the swap space size

It's a best practice that swap space is equal to two times the physical RAM, for up to 2 GB of physical RAM. For any amount greater than 2 GB, add an additional 1x physical RAM. It's also a best practice that swap space is never less than 32 MB.

Amount of system RAMRecommended swap space
2 GiB of RAM or less2 times the amount of RAM but never less than 32 MB
More than 2 GiB of RAM but less than 64 GiB0.5 times the amount of RAM
More than 64 GiBDepends on the workload or use case

Create a partition on your hard drive as swap space

1.    Use SSH to log in to the instance.

2.    List the available volumes:

$ sudo fdisk -l

3.    Select a device to partition from the list. In this example, use the device /dev/xvda.

$ sudo fdisk /dev/xvda

4.    Create a new partition:

\-> n

5.    Select a partition type. In this example, use primary:

\-> p

6.    Assign the partition number. In this example, use partition 2:

\-> 2

7.    Press Enter to accept the default of "First sector."

8.    Enter the size of the swap file. For this example, there is 2 GB of RAM. The partition that you created is 4 GB (specified as +4G).

\-> +4G

9.    Save and exit:

\-> w

Set up the swap area

1.    Use the partprobe command to inform the OS of partition table change:

$ partprobe

2.    Use the swap partition that you created in the preceding steps to set up a Linux swap area. In this example, the swap partition is /dev/xvda2.

$ mkswap /dev/xvda2

3.    Add the partition as swap space:

$ sudo swapon /dev/xvda2

4.    Show the current swap space:

$ sudo swapon -s

The output looks similar to the following example:

Filename                  Type        Size           Used    Priority
/dev/xvda2                partition   4194300        0       -1

5.    After the reboot, complete the following steps to make the swap memory allocation permanent:
Print the UUID from the blkid command:

$ sudo blkid

Copy the UUID of the swap partition from the preceding output. Use a text editor, such as vi or nano, to add the following to the /etc/fstab file:

UUID=xxxxxxx none swap SW 0 0

6.    Save the file, and then perform a reboot.

After you reboot the OS, you can use the free -h command to verify that the swap memory allocation persists.

Related information

Swap space (on the Red Hat website)

AWS OFFICIALUpdated 9 months ago