As a developer, have you ever faced a situation where your server/PC gets crashed or becomes completely unresponsive , during some high intensity work like installing some packages or high memory usage situation?
Sometimes, the server gets frozen, and you are not able to get access until manually rebooting if its a physical server or terminating the instance in the case of a cloud server. If you have ever encountered such a situation, and if that's not something to do with your action or a hardware failure, then, it might probably be because of exhaustion of your Physical RAM in your server for the process that you were running.
Although modern OS like Linux has a feature called Out of Memory (OOM) Killer Activation, which is triggered to identify and terminate one or more processes or the whole system to free up memory and prevent a complete system freeze or crash, sometimes even this service is not able to completely avoid this situation and the situation of freezing or crashing of system is seen. When your running process completely uses up RAM , then the operating systems triggers OOM killer, which tries to close unused process , but sometime even triggering OOM killer services fails , as it also requires small portion of RAM to be free, resulting into crashing of server and requiring you to manually boot or termination of system to get the access back.
So , to prevent such freezing of the server or also to optimize the performance of RAM , it is generally recommended to allocate SWAP memory.
Swap memory, sometimes also referred to as swap space, is an extension of a server’s RAM , by allocating a certain space on the hard drive or Solid State Drive (SSD). In this case, When the available RAM is exhausted, it swaps data between RAM and the swap space/memory. This mechanism, known as swapping, protects from such crashes and also help to manage memory efficiently.
Swap memory utilizes storage of hard drive of your computer which is dedicatedly allocated for RAM to store some data when the memory can no longer hold the Data in RAM. Swap memory is important for systems with restricted or limited amount of RAM or those executing memory-intensive tasks which prevents from crashing.
Now we will walk you through the Steps to allocate memory in your ubuntu machine:
Before actually, starting the process to add memory , first check if swap memory is already allocated, for this you can go to terminal in your ubuntu machine and type command below:
$ free -m
You can see the SWAP memory is 0 now. Hence , the first row with title Mem: is the RAM capacity, the second one with title Swap: is swap memory .
Now, we are targeting to allocate some memory in SWAP space as well.
OR another way to check the SWAP memory is to run command
$ sudo swapon --show
If the output is empty then you have not allocated swap memory in your server.
Now the next step is , to check the available disk (hard disk ) size, before allocating the memory. We can run following command to check the available disk space.
$ df -h
You can see multiple rows, which can be confusing , the actual disk size of your server is the row, with /(only slash) under mounted heading of output is your hard drive space . Here in the above case only 6.2Gb is available of Hard Drive.
The next step is to now actually allocate a swap file with the size equal to your desired size of SWAP.
Although there is no one size fits all on this, it depends on your size and requirements. A good rule of thumb is anything equal and between double the size of your RAM can be optimum.
But it depends on your requirements. Generally for systems with sufficient RAM i.e.(8 GB or more), having a swap space size equal to the amount of RAM might be adequate i.e. for 8GB RAM 8GB of SWAP to allocate. In some cases, having a swap space slightly larger than the RAM can also be useful specially if your RAM is less than 8GB , for handling certain scenarios to prevent problem like hibernation or freezing of server can be prevented to some extent.
Decisions for SWAP to take can be considered based on following considerations:
Now as we can see that there is less available space in our case as seen above:
Since we have a RAM of 4Gb so , in this article we shall allocate 4Gb of SWAP memory .
Considering above rule of thumb you are free to allocate as per your own preferences.
$ sudo fallocate -l 4G /swapfile
This function creates a file with 4Gb size. you can check the completion of this step using
$ ls -lh /swapfile
The output looks :
Until now only the swap file is allocated with a size of 4GB but the process is not completed yet, it is temporary
Now the first step after creating the swap file is to limit the permission so that only root privilege can access the file .
$ sudo chmod 600 /swapfile
We can mark the file as Swap memory using command
$ sudo mkswap /swapfile
The output will look like this:
Until now, in step 1 we created file, step 2 we maked the file as SWAP , now activate this file to make it memory.
$ sudo swapon /swapfile
This command will enable the swapfile.
Now you can check or verify if SWAP is enabled by using command discussed above:
$ sudo swapon --show
Also you can check using free -m
Since until now we have just allocated this amount of Space for RAM but it is temporary i.e. only for the current session. If we reboot the server then the swap memory will be gone, so we need to make it permanent .
We can make the swap file permanent by adding it to the /etc/fstab file:
$ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Congratulations !! you have now permanently completed allocation SWAP memory for efficent RAM performance.
This is final step for most of the case. If you are now tired and don't want to understand more, you can go to other blogs.
If you want to know more about swapiness and how RAM and hard drive work to prioritize one over other we will discuss on it in brief:
The "swappiness" parameter in Ubuntu controls the tendency of the kernel to move processes out of physical memory (RAM) or onto the swap space. It ranges from 0 to 100, where 0 instructs the kernel to keep more data in-memory(RAM) and use swap memory only when absolutely necessary, and higher values (close to 100) make the kernel more likely to swap memory whenever possible.
Choose the Right Swappiness Value:
To Sum up
Lower Values (0-10): If you have plenty of RAM and want to keep more processes in memory.
Default Value (60): A good balance for most systems.
Higher Values (80-100): If you have limited RAM and want to prioritize swapping.
You even can experiment with different values to find the setting that best suits your system and workload.
Setting the swappiness value too low might lead to excessive RAM usage, while setting it too high might result in unnecessary swapping and hence low performance as Hard drive is slower than RAM.
The default value is 60 which you can check from command line:
$ cat /proc/sys/vm/swappiness
You can adjust the swappiness value to your desired value using simple command below e.g in our case if we adjust it to 20 then
$ sudo sysctl vm.swappiness=20
Similar to allocation of memory in file is temporary and needs to make permanent, swapiness also requires to make it permanent.
Until this point, we have configured swapiness value to 20 in this case, which is valid temporarily i.e. as discussed above only until the reboot, so we need to adjust it permanently.
Following steps are to be performed
$ sudo nano /etc/sysctl.conf
This command will open up a file in your command line text editor, we add the following line to bottom of file:
vm.swappiness=20
We save the file and exit from the terminal.
The file /etc/sysctl.conf is a system wide configuration which is applied by the kernel of the OS when the system boots .i.e.
To make settings persist in reboots. The sysctl command reads the configuration files and updates the kernel parameters accordingly during the booting process.
With this article you have successfully allocated swap memory with swappiness value of 20. Which will be high performance and this can be altered as per your requirement of usage of server.
This can be done in case you are running out of memory errors or your server goes on hibernation once in a while with low memory availability.
If still allocating memory swap, still doesn't solve the issue of hibernation or freezing of server then you will need to upgrade your server or go to technician or report to your vendor