Lightweight Memory Leak Debugging on Linux

Lightweight memory leak debugging on linux

Surprisingly, I was unable to find anything like Microsoft's UMDH in open-source domain or available for immediate download. (I also looked at Google Heap Leak Checker but it is more like Valgrind rather than to UMDH). So I ended up writing the tool myself using malloc instrumentation project as a reference point:

https://github.com/glagolig/heapwatch

The tool has number of limitations, but it worked just fine for my purposes.

Memory leak detectors for C?

second the valgrind... and I'll add electric fence.

Problem with memory leak check tools in Linux

Boehm GC is a garbage collector, similar to the garbage collection in Java. The other tools you mention are designed to warn you about leaks, such that you can take corrective action. Garbage collection is designed to find and recover no-longer used allocations, as the program runs. Example (from wikipedia page):

#include <assert.h>
#include <stdio.h>
#include <gc.h>

int main(void)
{
int i;

GC_INIT();
for (i = 0; i < 10000000; ++i)
{
// GC_MALLOC instead of malloc
int **p = GC_MALLOC(sizeof(int *));
int *q = GC_MALLOC_ATOMIC(sizeof(int));

assert(*p == 0);
// GC_REALLOC instead of realloc
*p = GC_REALLOC(q, 2 * sizeof(int));
if (i % 100000 == 0)
printf("Heap size = %zu\n", GC_get_heap_size());
}

// No free()

return 0;
}

Personally there's something about using garbage collection in C or C++ that makes me quite uneasy. For C++ "Smart pointers" are the way to go in my opinion in scenarios where ownership is unclear (although you might want to have a thing about why it's unclear in your design) and for help with exception safety (E.g. what the now deprecated std::auto_ptr was designed for)

As for the leak detectors you can add:

  • ccmalloc
  • dmalloc
  • NJAMD (Not just another memory debugger)
  • mpatrol
  • YAMD (yet another malloc debugger)

To your list of Linux ones.

Related memory checking tools, but not leaks:

  • Electric fence

Memory debugger with ptrace

It is not practical to use a debugger and trap malloc/free calls for a few of reasons:

  1. The overhead of switching from one process to the other one is just to great on nontrivial programs.

  2. You'll end up spending a similar amount of memory to store ownership information than with other methods. (This is what I actually wanted to improve)

  3. There are quite a few functions that work the heap, and it could be easy to miss some.

Applications for detecting memory leaks

Maybe this wikipage looks useful for you:

Memory_debugger

I never use this kind of program before, and I have Windows, so i don't know which is good, but maybe you found one.

How to make valgrind displaying the memory status for daemon application?

According to the 3.8.0 valgrind manual, there is no such option for the core or for memcheck, and no mention on periodic or triggered behaviour in the signal section. valgrind can't do it out of the box.

While you might write a valgrind tool to do the job, you might re-think the approach and clarify what kind of memory usage you want, and if the system statistics might be sufficient for your task.



Related Topics



Leave a reply



Submit