When the system runs low on memory we need to creates temporary storage space using a swap file.
To add swap space to an EC2 instance, you can follow these steps
4. Set the appropriate permissions on the swap file:
For Example:1. Connect to your EC2 instance using SSH or a remote desktop connection.
2. Check the current disk usage and available space by running the df -h command.
2. Check the current disk usage and available space by running the df -h command.
3. Create a swap file by executing the following command:
sudo fallocate -l <swap_size> / swapfile
Replace <swap_size> with the desired size for your swap space, such as 1G for 1 gigabyte.
4. Set the appropriate permissions on the swap file:
[sudo chmod 600 / swapfile/pre]
5. Set up the swap space using the following command:
sudo mkswap / swapfile
6. Enable the swap space with the following command:
sudo swapon / swapfile
7. To make the swap space persistent after a system reboot, open the / etc / fstab file using a text editor:
sudo nano / etc / fstab
8. Add the following line to the end of the file:
/ swapfile none swap sw 0 0
9. Save the changes and exit the text editor.
10. Confirm that the swap space is active by running the sudo swapon --show command.
11. Verify the total available swap space by executing free -h.
Let's say we want to add a 2GB swap file to an EC2 instance:
sudo fallocate -l 2G / swapfile
sudo chmod 600 / swapfile
sudo mkswap / swapfile
sudo swapon / swapfile
sudo nano / etc / fstab
Add the following line at the end of the / etc / fstab file:
/ swapfile none swap sw 0 0
Save the changes and exit the text editor by pressing Ctrl+X, then Y, and finally Enter.
Finally, you can check the swap space with:
sudo swapon --show
free -h
These steps will create and enable a swap file on your EC2 instance, providing additional virtual memory.
Comments (0)