What is memory leak?

Memory leak is a phenomenon where a running C/C++ program or a running process or thread dynamically allocates memory block from the heap but fails to free the memory block when it no more requires the memory. This happens due to programmatic error where the handle to the allocated memory block gets lost.

Over a time if the same programmatic entity (a process, or a thread or a function) comes into action repeatedly and leaks memory, all the free memory of the systems goes away and eventually the systems throws the dreaded “Out Of Memory” and crumbles down. Let’s take an example as below:


#include
#include
#include 

int main(int argc, char **argv)
{
char *mem = (char *) malloc(20);

memset(mem, 0, 20);

return 0;
}

The source code can be found here as well: memory_leak.c

In the above program notice that the main() function (which is the only thread in this program) allocates a chunk of 20 bytes using malloc() memory allocator. Now, malloc() allocates memory from the heap. Heap memory doesn’t get released automatically once allocated by a programming entity, like a function/thread. We need to explicitly call free() on the memory handle (in this case “mem” pointer) to release the allocated memory. So once the main() program exits there is no way the system can release and get back this 20 bytes of memory and hence it is lost. Now, imagine this memory_leak program is run from a script within an infinite loop (won’t make much sense but you will get an idea how 20 bytes can cause a system crash). So every time memory_leak program runs, it takes away 20 bytes from the free memory pool of the system. If this keep on happening in the system, after a while all the free memory would be blocked by memory_leak program and hence, there is no handle to the lost memory it cannot be recovered. As a result the system would have no memory to run anything anymore and would die with a “Out of memory” message.

How to rectify this problem?

The only way to rectify memory leak problem is to detect it and apply a corresponding free() call to the memory handle of the allocated memory block. Let’s look at the below listing:


#include
#include
#include 

int main(int argc, char **argv)
{
char *mem = (char *) malloc(20);

memset(mem, 0, 20);

free(mem);

return 0;
}

Now, in this version we have added a free() function call to the memory pointer “mem”. What free does is just the opposite of malloc(). Free simply returns the memory allocated by malloc() back to the system memory pool.

How to detect or debug memory leak problem?

Memory leak is a big problem in programming world. There are static and runtime tools available to detect memory leak. But needles to say, even after having multiple memory leak tools in place, finding and fixing memory leak could be a daunting task in a large project.

Static tools

Coverity, SVACE etc are toold which can be run over a source code to find out memory leak. Though these tools are not restricted to issues only with memory leak. This is called static code analysis where the source code is not running on the target but being analysed offline.

Runtime tools

Among many runtime memory leak tools the one I like the most is called valgrind. It analyses a program in runtime and gives the possible memory leaks occurred due to the program run. To know how to run valgrind on a running program please look into my previous post How to detect memory leak in c using valgrind?

Leave a Reply