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:
The overhead of switching from one process to the other one is just to great on nontrivial programs.
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)
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
What Is the Performance Penalty of C++11 Thread_Local Variables in Gcc 4.8
Why Does the Free() Function Not Return Memory to the Operating System
Why Is a NaïVe C++ Matrix Multiplication 100 Times Slower Than Blas
Why Would Connect() Give Eaddrnotavail
Force to Link Against Unused Shared Library
Why Does Poll Keep Returning Although There Is No Input
Are There Any Way to Link My Program with Wine-Compiled Part
How to Multicast with Ipv6 Udp Socket in C/C++ on Linux
Deploying Qt Applications in Linux Properly
Wait Until User Presses Enter in C++
How to an Share Address Mapping Between Two Unrelated Processes on Linux
Why Can't I Add a Qgridlayout to a Qmainwindow
Template Singleton Base Class in Shared Object
Statically Linking System Libraries, Libc, Pthreads, to Aid in Debugging
Why Isn't Malloc Filling Up Memory
Differencebetween Crtbegin.O, Crtbegint.O and Crtbegins.O
How to Correctly Interpose Malloc Allowing for Ld_Preload Chaining