Swipe left or right to navigate to next or previous post
This blog post is about how to increase the swap memory of the linux. The steps to set the swap memory is similar in all the linux Operating Systems. However, following steps are based on Ubuntu OS
Computer random access memory (RAM) is one of the main component of the computer system. RAM stores and provides access to the running application data. RAM is one of the fastest storage used in computer. RAM stores the information that is actively used so that it can be accessed quickly. The size of RAM one needs depends on the no of concurrent program running in the system and the size of the individual program the system needs to run. As the size of the program increases, more RAM is needed in the system along with high spec CPU.
The size of the RAM is limited in computer system due to various reasons. One of the reasons is it one of the very expensive memory among other memory type. Another reason is that the server comes with the limited size of RAM with the basic or free plan like in AWS which provides up to 30GB of storage and 1GB of RAM. So, for such systems, there can be backup plan to store the currently processing data or program when the system runs of the RAM. One of the solution is to set the swap memory. It is not as good as RAM. But it stills works fine for low performance system.
Whenever there is deficient of memory in the computer system for the program, the Operating System borrows the space to be used as memory from the Hard Drive. This borrowed space is called a Swap memory.
Whenever the is not enough memory in the Ram, the memory management program looks for inactive blocks which has not been used for a long period of time. As soon as it finds such block, it shifts those blocks into swap memory and free the memory of ram to be utilized for some other program that need to be processed urgently.
Amount of RAM installed in system | Recommended swap space | Recommended swap space with hibernation |
---|---|---|
≤ 2GB | 2X | 3X RAM |
2GB – 8GB | = RAM | 2X RAM |
8GB – 64GB | 4G to 0.5X RAM | 1.5X RAM |
>64GB | Minimum 4GB | Hibernation not recommended |
Create a swap file that can be used as swap. Run the following command
sudo fallocate -l 4G /swapfile
Here 4G is the size of the swap file. You can set the size of swap file based on your necessity.
If fallocate is not installed or if you get an error message saying fallocate failed: Operation not supported then you can use the following command to create the swap file:
sudo dd if=/dev/zero of=/swapfile bs=4096 count=1048576
Only the root user is able to write to the swap file. so, update the permissions
sudo chmod 600 /swapfile
Use the mkswap utility to set up the file as Linux swap area:
sudo mkswap /swapfile
Enter the following command enables the swap file
sudo swapon /swapfile
To make the swap file permanent, open the /etc/fstab file and append the following line:
sudo nano /etc/fstab /swapfile swap swap defaults 0 0
To verify the swap file is active, use either the swapon or the free command as shown below:
sudo swapon --show
Sometime you mau want to remove the swap file. Run the following command to remove the swap
sudo swapoff -v /swapfile
Remove the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.
For this, run the following command
sudo rm /swapfile