$export MALLOC_TRACE=mymemory.log
3. 正常运行程序。此时程序中的关于内存分配和释放的操作都可以记录下来。
(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/linux/)4. 然后用mtrace使用工具来分析log文件。例如:
$mtrace testmem $MALLOC_TRACE
下面是具体一个例子,大家可以看一下。
[hwang@langchao test]$ cat testmtrace.c#include #include #include int main(){ char *hello; mtrace(); hello = (char*) malloc(20); sprintf(hello,"hello world!"); return 1;}[hwang@langchao test]$export MALLOC_TRACE=mytrace.log[hwang@langchao test]$ gcc testmtrace.c -o testmtrace[hwang@langchao test]$./testmtrace[hwang@langchao test]$ mtrace testmtrace mytrace.logMemory not freed:-----------------Address Size Caller0x08049860 0x14 at /usr/src/build/53700-i386/BUILD/glibc-2.2.4/csu/init.c:0