How do I allocate memory to work as swap space in an Amazon EC2 instance by using a swap file?
I want to allocate memory to work as a swap file in an Amazon Elastic Compute Cloud (Amazon EC2) instance. How do I do that?
"I want to allocate memory to work as a swap file in an Amazon Elastic Compute Cloud (Amazon EC2) instance. How do I do that?Short descriptionWhen physical RAM is already in use, Amazon EC2 instances use swap space as a short-term replacement for physical RAM.Contents of RAM that aren't in active use or that aren't needed as urgently as other data or instructions can be temporarily paged to a swap file. This frees up RAM for more immediate use.You can also create swap space on a partition. For more information, see How do I allocate memory to work as swap space on an Amazon EC2 instance using a partition on my hard drive?Note: It's a best practice to create swap space only on ephemeral storage instance store volumes.ResolutionBefore beginning this resolution, verify that your file system supports the usage of swap files.Note: Most of the common file systems (ext3, ext4, XFS) are supported. This may not be the same for other filesystems. It is the best practice to review the latest file system documentation.Calculate the swap space sizeFor information on how to calculate the size of your swap space, see the following:All about Linux swap space on the linux.com website.How much swap do I need? on the help.ubuntu.com website.Create a swap file1.    Use the dd command to create a swap file on the root file system. In the command, bs is the block size and count is the number of blocks. The size of the swap file is the block size option multiplied by the count option in the dd command. Adjust these values to determine the desired swap file size.The block size you specify should be less than the available memory on the instance or you receive a "memory exhausted" error.In this example dd command, the swap file is 4 GB (128 MB x 32):$ sudo dd if=/dev/zero of=/swapfile bs=128M count=322.    Update the read and write permissions for the swap file:$ sudo chmod 600 /swapfile3.    Set up a Linux swap area:$ sudo mkswap /swapfile4.    Make the swap file available for immediate use by adding the swap file to swap space:$ sudo swapon /swapfile5.    Verify that the procedure was successful:$ sudo swapon -s6.    Start the swap file at boot time by editing the /etc/fstab file.Open the file in the editor:$ sudo vi /etc/fstabAdd the following new line at the end of the file, save the file, and then exit:/swapfile swap swap defaults 0 0Related informationRHEL - Adding swap spaceFollow"