Swap space, an extension of physical memory, enables instances to manage memory resources efficiently, preventing performance degradation in situations of high demand. This guide outlines the steps to add swap space to an Amazon EC2 instance
Connect to EC2 Instance.
Before adding a swap, check the current disk space and available memory on your instance. we can use commands to check disk space and check memory.
Decide on the size of your swap space. For example, if we want to create a 1GB swap file, we can use the following commands.
|$ sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576|
Secure the swap file by adjusting permissions:
|$ sudo chmod 600 /swapfile|
Use the following command to mark the file as swap space.
|$ sudo mkswap /swapfile|
Activate the swap file with the following command.
|$ sudo swapon /swapfile|
Add the swap file to the
/etc/fstab file to ensure it’s mounted on the system reboot.
|$ echo ‘/swapfile none swap sw 0 0’ | sudo tee -a /etc/fstab|
Confirm that the swap space is available and active by running.
|$ sudo swapon –show|
The addition of swap space provides a pragmatic solution to handle fluctuations in memory demand, ensuring a seamless user experience.