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:



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.

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] [ ]
  2. Very Sleepy[ ]
  3. AMD CodeAnalyzer[ ]
  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.

1 comment:

  1. ان شركة عزل الخزانات تتميز بان لديها من العمال الافضل و اصحاب الخبرات الكبيرة و المتميزين في مجال العزل و التنظيف
    شركة عزل خزانات بالمدينة المنورة
    ان جميع الفنيين و المتخصصين يتميزون بالخبرة الكبيرة و الحرفية العالية حيت التدريب علي التغلب علي جميع المشكلات التي قد تواجه عملية العزل و التنظيف كما ان التنظيف و العزل يتم تحت اشراف هيئة من المهندسيين الكيميائين المتخصصين في اعمال لاعزل الكيميائي لضمان سلامة العزل و سلامة المواد المستخدمة اننا نستخدم افضل المواد و ءامنها لاننا
    عزل خزانات بالمدينة المنورة
    نتعامل مع المياه التي هي اساس الحياه ان المواد المستخدمة مصرح بها من المنظمات العالمية و معتمدة من منظمة الصحة العلمية حيث يتم استخدامها لعزل خزانات مياه الشرب و لذا فنحن نتمتع بالمصداقية و الثقة العالية من قبل عملائنا الاجلاء. اننا نستخدم احدث الاجهزة و المعدات و الادوات الحديثة و التي تستخدم اثناء عملية عزل الخزنات و ذلك لرفع مستوي الخدمة من حيث الاداء و الجودة و ايضا تقليلا للوقت المستخدم اثناء العملية لا تتردد في الاستعانة بنا
    عزل اسطح بالمدينة المنورة
    فنحن الافضل و الاميز في خدمات العزل .