Linux Memory Management
My Linux server started having problems recently. All I knew is, websites hosting on the server showing ‘Service Not Available’ Error on the website.
After a little dig into log files, I found following line in /log/apache2/error.log file at almost the same time stamp when service was not available for the websites.
[crit] Memory allocation failed, aborting process.
This shows there is memory issue on the server. There are two possible solutions that I could think of
- Add more Memory(RAM) to the server
- Find out the process that is consuming all the RAM and get it fixed
Second solution is going to take time and I am still working on that. However, first solution is temporary if there is really a process that is going out of control at certain time.
To allow more memory for the server, Adding a RAM is adding a hardware to the server that not only causes more money but also not needed at the time. So, I decided to add SWAP file to the server. If you don’t know about SWAP file, please Google it. Here I am going to write, how to add and enable a swap file
Following are the commands you might need in your SSH window:
- To see if there is any swap already configured on the server
sudo swapon -s
If it shows only header like following, it means there is no swap file set at the moment
Filename Type Size Used Priority
- Alternate command for this is to see the free memory on the server like following,
it shows not only the RAM usage but also swap files usage. In our case like above, it showed “swap 0 0 0” that means no swap file available
Above command also tells how much RAM we have. So, it is recommended to use either same size of SWAP to RAM of use SWAP as double to RAM.
- In order to check all the space on the server, use following command
It will help to decide how much space can be allocated for SWAP file.
- We decided to use 4GB for SWAP, following commands are to consider 4 GB of SWAP file, you may use alter it according to your own decision.
sudo fallocate -l 4G /myswapfile
There is an alternate way to specify more details for the file, but I decided to use a quicker way here. I will write in next article for the detailed alternate way of doing the same.
- To confirm about the result of above command, use following command
ls -lh /
A list like following will confirm everything is fine so far
-rw-r--r-- 1 root root 4.0G May 02 14:23 /
- Lock the permissions on the file using following command as no one other than root should have access to this file
sudo chmod 600 /myswapfile
To confirm its result, repeat previous command ‘ls -lh /myswapfile’
- Now is the step to enable SWAP file to be used, execute following commands
sudo mkswap /myswapfile
sudo swapon /myswapfile
Now verify the impact of above commands with very first command list above as 'sudo swapon -s'
By this time, we have created and enabled SWAP file. However, if we reboot server, we will lose the swap file as server doesn't know to automatically enable it on boot.
Use following command to open configuration file
sudo nano /etc/fstab
At the end of the file, add following command and save it
/myswapfile none swap sw 0 0
In case of any query or confusion, please comment on the post and we will be able to find solution.