valgrind一、工具获取二、valgrind组成1、Memcheck 2、Callgrind3、Cachegrind 4、Helgrind5、Massif6、lackey7、nulgrind8、DRD二、安装三、使用(官网http://valgrind.org/docs/manual/quick-start.html#quick-start.intro)1、常规检测内存使用有无异常A、源码如下四、深入研究1、massif 用法2、unbuntu 下有个漂亮的可视化工具,查看massif输出
valgrind组成Memcheck一切对malloc()/free()/new/delete的调用都会被捕获。所以,它能检测以下问题 :
Callgrindxxxxxxxxxx./configure, make, make install
x
void f(void) { int* x = malloc(10 * sizeof(int)); x[10] = 0; // problem 1: heap block overrun } // problem 2: memory leak -- x not freed int main(void) { f(); return 0; }xxxxxxxxxx[wishcell@localhost c]$ gcc -g ./tt.c -o tt[wishcell@localhost c]$ valgrind --leak-check=yes ./tt==6963== Memcheck, a memory error detector==6963== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.==6963== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info==6963== Command: ./tt==6963====6963== Invalid write of size 4==6963== at 0x40054B: f (tt.c:6)==6963== by 0x40055B: main (tt.c:11)==6963== Address 0x51f9068 is 0 bytes after a block of size 40 alloc'd | 第6行所用到的内存是在刚==6963== at 0x4C29BE3: malloc (vg_replace_malloc.c:299) | 刚分配的40个字节之后。==6963== by 0x40053E: f (tt.c:5) | 意思是说这不是分配的内存==6963== by 0x40055B: main (tt.c:11)==6963====6963====6963== HEAP SUMMARY:==6963== in use at exit: 40 bytes in 1 blocks==6963== total heap usage: 1 allocs, 0 frees, 40 bytes allocated==6963====6963== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1==6963== at 0x4C29BE3: malloc (vg_replace_malloc.c:299)==6963== by 0x40053E: f (tt.c:5) |这里说第5行申请的内存全部泄漏掉了。因为没有释放操作==6963== by 0x40055B: main (tt.c:11)==6963====6963== LEAK SUMMARY:==6963== definitely lost: 40 bytes in 1 blocks==6963== indirectly lost: 0 bytes in 0 blocks==6963== possibly lost: 0 bytes in 0 blocks==6963== still reachable: 0 bytes in 0 blocks==6963== suppressed: 0 bytes in 0 blocks==6963====6963== For counts of detected and suppressed errors, rerun with: -v==6963== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)[wishcell@localhost c]$ cat tt.c -n 1 #include <stdlib.h> 2 3 void f(void) 4 { 5 int* x = malloc(10 * sizeof(int)); 6 x[10] = 0; // problem 1: heap block overrun 7 } // problem 2: memory leak -- x not freed 8 9 int main(void) 10 { 11 f(); 12 return 0; 13 }[wishcell@localhost c]$
void f(void){ int* x = malloc(10 * sizeof(int)); x[10] = 0; // problem 1: heap block overrun} // problem 2: memory leak -- x not freedint main(void){ f(); return 0;}xxxxxxxxxx[wishcell@localhost c]$ gcc -g ./tt.c -o tt[wishcell@localhost c]$ valgrind --tool=massif ./tt[wishcell@localhost c]$ valgrind --tool=massif ./tt==7238== Massif, a heap profiler==7238== Copyright (C) 2003-2015, and GNU GPL'd, by Nicholas Nethercote==7238== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info==7238== Command: ./tt==7238====7238==执行后会得到一个文件:massif.out.7238,用命令 ms_print 查看之,可以看到内存的收支情况:[wishcell@localhost c]$ ms_print massif.out.7238--------------------------------------------------------------------------------Command: ./ttMassif arguments: (none)ms_print arguments: massif.out.7238-------------------------------------------------------------------------------- B 56^ : | : | : | : | : | : | : | : | : | : | : | : | : | : | : | : | : | : | : | : 0 +----------------------------------------------------------------------->ki 0 106.2Number of snapshots: 2 Detailed snapshots: []-------------------------------------------------------------------------------- n time(i) total(B) useful-heap(B) extra-heap(B) stacks(B)-------------------------------------------------------------------------------- 0 0 0 0 0 0 1 108,733 56 40 16 0[wishcell@localhost c]$ xhttp://blog.csdn.net/unix21/article/details/9330571