4 ms·
For better or worst, I prefer to write my own memory leak checker for most of the projects I worked on. After did it a few times, it normally take less than 1
by tkinom 11y ago
For better or worst, I prefer to write my own memory leak checker for most of the projects I worked on.
After did it a few times, it normally take less than 1 days to put in the code - basically wrapper for malloc, free, etc, new, delete. Add some table code to track bytes alloc/ free per filename/line#.
It must have a very concise high level table dump memory alloc/free associate with file:lineno.
file:lineno: #Alloc_ByteCnt #Free_ByteCnt
Advantages:
It works very well on app that need to runs forever. One can easily track memory usage/leakage over time, but adding a trigger to dump the memory table without killing the app. The trigger can be TestAPI call or external URL.
If you have a full functional regression test coverage system in place (All my projects has them), one can easily trigger the memory table dump along with certain test points. Fairly easy to ID memory leak associate with very high level functionalities such as
* certain image load trigger 200K leak per test loop.
* Certain URL access trigger file:line leak 64 bytes per test loop
* , etc
Once all these setup in place, memory leak are fairly trivial to solved. I have not seen any user space memory leak that take more than 1 hours to solve and verify.
The most complex memory leak I had to debug was the EGL app that trigger EGL kernel driver memory leak at ~20MB per over night testing. To debug that was much harder.....
Because that was the kernel/system memory is leaking not just user APP memory.
- Jabbles 11y agoMemory profilers are far cheaper to use than valgrind.
- kazinator 11y agoValgrind can also dump leak information while the program is running. For that, you have to use the Valgrind API. There is a request for it. #include <valgrind/memcheck.h> ... // in the code: VALGRIND_DO_LEAK_CHECK; I like to put #ifdef CONFIG_VALGRIND_SUPPORT around this stuff so it can be compiled out. The Valgrind API call macros expand to some machine code instruction patterns which execute harmlessly on a regular CPU, and have no effect, but are recognized as triggers by the Valgrind execution core. These instructions add bloat and burn cycles. Valgrind has the advantage that it has a real leak checker: i.e. reports of still-allocated memory that is definitely unreachable by any pointer. It can also report blocks which are reachable only by pointers into their interior, not to their base address. Having the stack trace for each allocation is also nice. If something is leaked, it can sometimes be useful to know where it was allocated (by what chain of calls). Of course you might know that every dynamic struct foo came from foo_alloc. But who called foo_alloc for a given leaked struct foo? And who called that caller?
- Too 11y agoValgrind will also warn you if you try to use unassigned memory, i don't think wrapping malloc could possibly to that.