作者:帅得不敢出门 C++哈哈堂 31843264 转载请保留此信息
valgrind
Valgrind is an instrumentation framework for building dynamic analysis tools. There are Valgrind tools that can automatically detect many memory management and
threading bugs, and profile your programs in detail. You can also use Valgrind to build new tools.
下载:
svn co svn://svn.valgrind.org/valgrind/trunk valgrind
安装:
cd valgrind
./autogen.sh
./configure --prefix=...
make
sudo make install
使用方法:要检测的程序必须加-g选项,推荐使用-O0,不优化
内存检测:
如果你运行程序时命令是这样的:
myprog arg1 arg2
检测时改成这样:
valgrind --leak-check=yes myprog arg1 arg2 ,默认是memcheck
valgrind --tool=cachegrind myprog arg1 arg2 ,这个调用的则是cachegrind,输出结果在当前目录下的cachegrind.out.
pid为程序的pid
Valgrind工具包包含多个工具,如Memcheck,Cachegrind,Helgrind, Callgrind,Massif,你可以用上面的--tool=来指定,指定时没有大写
exp-ptrcheck, a heap, stack and global array overrun detector
valgrind --tool=exp-ptrcheck --log-file=val.log ./fbv -g /windows/F/photos/102_4085.JPG
输出如下:
==17627== exp-ptrcheck, a heap, stack and global array overrun detector
==17627== NOTE: This is an Experimental-Class Valgrind Tool
==17627== Copyright (C) 2003-2010, and GNU GPL'd, by OpenWorks Ltd et al.
==17627== Using Valgrind-3.7.0.SVN and LibVEX; rerun with -h for copyright info
==17627== Command: ./fbv -g /windows/F/photos/102_4085.JPG
==17627== Parent PID: 2448
==17627==
==17627==
==17627== For counts of detected and suppressed errors, rerun with: -v
==17627== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
valgrind --tool=callgrind ./fbv -g /windows/F/photos/102_4085.JPG 同时另一个命令行下运行:
callgrind_control -b
输出如下:
PID 17678: ./fbv -g /windows/F/photos/102_4085.JPG [requesting 'Status'...]
Frame: Backtrace for Thread 1
[ 0] rotate (1 x)
[ 1] show_image (1 x)
[ 2] main (1 x)
[ 3] (below main) (1 x)
[ 4] 0x080495a0 (1 x)
[ 5] 0x00000850
程序结束后,产生文件callgrind.out.
callgrind_annotate [options] callgrind.out. 产生较精简的函数summary
Massif is a heap profiler. It measures how much heap memory your program uses. This includes both the useful space,
and the extra bytes allocated for book-keeping and alignment purposes. It can also measure the size of your
program's stack(s), although it does not do so by default.
valgrind --tool=massif ./fbv -g /windows/F/photos/102_4085.JPG 生成堆使用情况的log
ms_print massif.out.19747 对log做些处理产生可易理解的信息
--------------------------------------------------------------------------------
n time(i) total(B) useful-heap(B) extra-heap(B) stacks(B)
--------------------------------------------------------------------------------
46 444,727,063 12,190,488 12,190,464 24 0
47 444,727,955 12,715,808 12,715,776 32 0
48 446,465,375 12,190,488 12,190,464 24 0
49 446,466,273 15,336,224 15,336,192 32 0
50 456,720,045 15,336,224 15,336,192 32 0
100.00% (15,336,192B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->61.54% (9,437,184B) 0x8049820: show_image (in /home/zengming/workspace/fbv/fbv)
| ->61.54% (9,437,184B) 0x804A87D: main (in /home/zengming/workspace/fbv/fbv)
............
DHAT is a tool for examining how programs use their heap allocations.
valgrind --tool=exp-dhat ./fbv -g /windows/F/photos/102_4085.JPG
==19813==
==19813== ======== SUMMARY STATISTICS ========
==19813==
==19813== guest_insns: 456,751,347
==19813==
==19813== max_live: 15,336,192 in 4 blocks
==19813==
==19813== tot_alloc: 16,539,148 in 25 blocks
==19813==
==19813== insns per allocated byte: 27
==19813==
==19813==
==19813== ======== ORDERED BY decreasing "max-bytes-live": top 10 allocators ========
==19813==
==19813== -------------------- 1 of 10 --------------------
==19813== max-live: 9,437,184 in 1 blocks
==19813== tot-alloc: 9,437,184 in 1 blocks (avg size 9437184.00)
==19813== deaths: 1, at avg age 456,008,575 (99.83% of prog lifetime)
==19813== acc-ratios: 0.25 rd, 1.00 wr (2,359,296 b-read, 9,437,184 b-written)
==19813== at 0x4026BB4: malloc (vg_replace_malloc.c:236)
==19813== by 0x8049821: show_image (in /home/zengming/workspace/fbv/fbv)
==19813== by 0x804A87E: main (in /home/zengming/workspace/fbv/fbv)
...............
英文说明:
阅读(3703) | 评论(0) | 转发(0) |