How do I allocate memory to work as swap space on an Amazon EC2 instance using a partition on my hard drive?
I want to allocate memory to work as swap space on an Amazon Elastic Compute Cloud (Amazon EC2) instance using a partition on my hard drive. How do I do that?
"I want to allocate memory to work as swap space on an Amazon Elastic Compute Cloud (Amazon EC2) instance using a partition on my hard drive. How do I do that?Short descriptionTo allocate memory as swap space, do the following: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. Swap space can be used for instances that have a small amount of RAM, but isn't 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, consider increasing your instance size.ResolutionCalculate the swap space sizeIt's a best practice that swap space is equal to 2 times the physical RAM, for up to 2 GB of physical RAM. For any amount above 2 GB, add an additional 1x physical RAM. It's a best practice that swap space is never less than 32 MB.Amount of system RAMRecommended swap space2 GiB of RAM or less2 times the amount of RAM but never less than 32 MBMore than 2 GiB of RAM but less than 64 GiB0.5 times the amount of RAMMore than 64 GiBDepends on the workload or use caseCreate a partition on your hard drive as swap space1..    Log in to the instance using SSH.2..    List the available volumes:$ sudo fdisk -l3..    Select a device to partition from the list. In this example, use the device /dev/xvda.$ sudo fdisk /dev/xvda4..    Create a new partition:-> n5..    Select a partition type. In this example, use primary:-> p6..    Assign the partition number. In this example, use partition 2:-> 27..    Accept the default of "First sector" by pressing Enter.8..    Enter the size of the swap file. For this example, there is 2 GB of RAM, and the partition created is 4 GB (specified as +4G).-> +4G9..    Save and exit:-> wSet up the swap area1..    Use the partprobe command to inform the OS of partition table change:$ partprobe2..    Set up a Linux swap area using the swap partition you created in the preceding steps. In this example, the swap partition is /dev/xvda2.$ mkswap /dev/xvda23..    Add the partition as swap space:$ sudo swapon /dev/xvda24..    Show the current swap space:$ sudo swapon -sOutput similar to the following appears:Filename                Type        Size      Used    Priority/dev/xvda2              partition   4194300   0       -15..    Make the swap memory allocation permanent after reboot with the following command:Note: If xvda2 isn't your swap device name, then replace this term with the swap device name in your environment.$ cp /etc/fstab /etc/fstab_$(date +%Y%m%d%H%M%S)$ cat <<EOF >> /etc/fstab`sudo blkid /dev/xvda2 | grep -Eo '[[:alnum:]]{8}(-[[:alnum:]]{4}){3}-[[:alnum:]]{12}'` swap swap defaults 0 0EOF$ rebootRelated informationSwap spaceFollow"