利用linux的mtrace命令定位記憶體洩露(Memory Leak)
阿新 • • 發佈:2018-12-02
1、安裝mtrace工具
centos : sudo yum install glibc-utils
2、mtrace工具使用
/************************************************************************* > File Name: mtrace.c > Author: jinshaohui > Mail: [email protected] > Time: 18-11-07 > Desc: ************************************************************************/ #include<assert.h> #include<string.h> #include<stdlib.h> #include<stdio.h> #include<mcheck.h> int main() { int *p = NULL; setenv("MALLOC_TRACE","1.log",1); mtrace(); p = malloc(100); muntrace(); return; }
編譯的時候需要加-g
[[email protected] work]$ gcc -g mtrace.c [[email protected] work]$ [[email protected] work]$ ./a.out [[email protected] work]$ mtrace a.out 1.log Memory not freed: ----------------- Address Size Caller 0x0904c428 0x64 at /home/jsh/workspace/Linux-C-C--learning/C/work/mtrace.c:21
很容易看出23行存在記憶體洩漏。