Thursday, October 7, 2010

Memory leakage in Linux

What is Memory leakage?

Memory Leakage basically refers to a situation where the memory allocated to an application (can be a Database query as well) is not getting freed up. This can be due to bug in the program which utilize the memory resource. In my opinion, when an application closes, it should issue the proper exit statement so that it will free up the memory which it occupied and kill all its child process. 
So this Memory leakage is basically an application issue, and any leaked memory will be freed up after the application is killed or stopped.


How to find the Memory leakage?

We would first need to determine which application/process is consuming more memory.  This can be done through data gathering. We need to periodically check what is running on a server and what is the Memory utilization . With the time when the server runs out of available memory to allocate, the leaking application might crash or it may even crash the system.

Valgrind is a popular Open source tool available for detecting the memory leakage of an application (URL:  http://valgrind.org/ ). A sample Valgrind output attached.


How to fix it?

We need to correct the application from using more memory ( Done by a Programmer or DBA).  I don’t think we can do much on the server side since the job of kernel is release the requested memory by an application. Certain things we can do at Server end are, tuning some kernel parameter like restricting the number of child process forked by a single process, restricting number of process forked by an user etc. If the kernel have the intelligence to distinguish, which request for more memory is valid and which one causes memory leaks, it would be great but am not sure if it’s possible to do such level of tuning J


In Linux, Memory leakage can be found using the below given 'ps' command. This will give you an idea about Memory usage of each process in a sorted manner.
# ps aux --sort pmem

No comments:

Post a Comment