您的位置:首页 > 运维架构 > Linux

应用 Valgrind 发现 Linux 程序的内存问题

2011-06-24 11:22 816 查看
一,安装:

1、 到www.valgrind.org下载最新版valgrind-3.2.3.tar.bz2

2、 解压安装包:tar –jxvf valgrind-3.2.3.tar.bz2

3、 解压后生成目录valgrind-3.2.3

4、 cd valgrind-3.2.3

5、 ./configure --prefix=/data/home/nemo/tools/valgrind

6、 Make;make install

二。使用

1 valgrind不能attach到已经在运行的进程上(到目前为止在网上木有找到方法)

2 最基本的用法,在启动脚本加上 valgrind --tool=memcheck --leak-check=full --show-reachable=yes --log-file=$LOGFILE ./$pname $RUN_OPTIONS

3 --log-file是加上输出日志文件

4 如果日志太多,可查找“Invalid“ "ERROR SUMMARY"

进入到 tools/valgrind/bin 目录下(如果安装是默认的话就在/usr/local/bin)

执行 ./valgrind ls -l

如果显示:

==17343== Memcheck, a memory error detector

==17343== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.

==17343== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info

==17343== Command: ls -l

==17343==

total 220

-rwxr-xr-x 1 nemo users 40959 2010-08-31 15:11 callgrind_annotate

-rwxr-xr-x 1 nemo users 13662 2010-08-31 15:11 callgrind_control

-rwxr-xr-x 1 nemo users 32064 2010-08-31 15:11 cg_annotate

-rwxr-xr-x 1 nemo users 43514 2010-08-31 15:11 cg_merge

-rwxr-xr-x 1 nemo users 24211 2010-08-31 15:11 ms_print

-rwxr-xr-x 1 nemo users 8715 2010-08-31 15:11 no_op_client_for_valgrind

-rwxr-xr-x 1 nemo users 30703 2010-08-31 15:11 valgrind

-rwxr-xr-x 1 nemo users 19175 2010-08-31 15:11 valgrind-listener

==17343==

==17343== HEAP SUMMARY:

==17343== in use at exit: 12,539 bytes in 37 blocks

==17343== total heap usage: 320 allocs, 283 frees, 35,761 bytes allocated

==17343==

==17343== LEAK SUMMARY:

==17343== definitely lost: 72 bytes in 2 blocks

==17343== indirectly lost: 240 bytes in 20 blocks

==17343== possibly lost: 0 bytes in 0 blocks

==17343== still reachable: 12,227 bytes in 15 blocks

==17343== suppressed: 0 bytes in 0 blocks

==17343== Rerun with --leak-check=full to see details of leaked memory

==17343==

==17343== For counts of detected and suppressed errors, rerun with: -v

==17343== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 3 from 3)

表示安装ok了.

使用:http://www.ibm.com/developerworks/cn/linux/l-cn-valgrind/

http://baoer31.blog.163.com/blog/static/66036536200852665733158/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: