Wednesday, February 29, 2012

Memory leak detection in C,C++ codes and Apps

On of the major part of C/C++ native application development is the memory management.Allocation and Deallocation of memory on runtime and cleaning memory before exit is hard to keep in touch while development. If OOP is in the best use this can be minimized but still can be a issue. Specially while loading and using 3rd party dll and libraries can cause unknown issues.

Detection of leaks can be done in many ways,

  1. code level
  2. using detection tools.


Code level memory detection is supported by most compilers and there are many libraries to do so. This is almost done in debug mode.
_CRT library example of memory detection.

This is a set of libraries given my Microsoft to monitor memory status in the application:

Before using this you have to include following lines of codes:


#ifndef _CRTDBG_MAP_ALLOC
#define _CRTDBG_MAP_ALLOC
#endif _CRTDBG_MAP_ALLOC


#include
#include

Then after you have to go to a certain function call and check whether there was a memory leak within execution of those lines of codes.


_CrtMemState s1, s2, s3;
_CrtMemCheckpoint( &s1 );
   //Run the required function call here...
_CrtMemCheckpoint( &s2 );
if ( _CrtMemDifference( &s3, &s1, &s2) ){
_CrtMemDumpStatistics( &s3 );}

This will print the memory leak details on the debug window.
Ex:

0 bytes in 0 Free Blocks.
481371 bytes in 867 Normal Blocks.
1284 bytes in 4 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 1056688 bytes.
Total allocations: 2270062 bytes.


You can Google and find out what the exact meaning of those block types. But what we most identify are the Normal Blocks.
So, to avoid memory leaks, you have to go in to that function and add more of those code and find out the last set of minor codes which cause the memory leak and fix it. Unfortunately there is no easy method to fix those. Because all those are due to you are forgetting to deallocate memory objects allocated earlier.

There are many Memory detection and monitoring tools.Most of those tools are hardware dependent. Because in order to monitor an executing application, it needs to go deep into the application execution. So tools become best when they can go deep in to the Hardware Layer. There for most memory detection tools are products of processor manufacturer like Intel and AMD.


  1. Valgrind [only for linux but superb] [ http://valgrind.org/ ]
  2. Very Sleepy[ http://www.codersnotes.com/sleepy ]
  3. AMD CodeAnalyzer[ http://developer.amd.com/tools/codeanalyst/pages/default.aspx ]
  4. Microsoft Task Manager [a general tool, but to that informative]
You can run your app using those tools and they will give you how each functions, threads, objects are occupying memory in run time. So you can go back to the code and identify those locations and fix.

Memory leak detection, locating and fixing is a time consuming process and it could not be 100% fixable task. But you can identify re-occurring leaks and fix those at first. This will make application long last on run and more reliable. So the application will make more stable. Some memory leaks which occurs on initial memory allocation will not continue to grow through continuously, so may not be that much critical.

So, finally its all about making your critical application more stable and run last long more reliably.


No comments:

Post a Comment