官网下载最新版valgrind:
http://valgrind.org/downloads/current.html#current
下载的压缩包,传到需要检测的服务器,解压:tar -jxf valgrind-3.13.0.tar.bz2
进入解压后的文件夹,执行:./autogen.sh
执行:./configure --prefix=/home/valgrind(安装路径)
执行:make
执行:make install
安装完毕,在文件夹内执行:./valgrind ls -l,验证是否安装成功
为了方便使用,可以将安装路径加入系统PATH:vim ~/.bashrc ,在最后一行添上: export PATH=/usr/local/bin:$PATH ,输入“source ~/.bashrc”命令,立即生效
使用时,先进入可执行程序所在目录,然后执行:valgrind -v --log-file=valgrind.log --tool=memcheck --leak-check=full --show-mismatched-frees=yes main(可执行程序名)
在生成的valgrind.log日志文件中,搜索LEAK SUMMARY可以看到如下汇总信息:
==42253== LEAK SUMMARY:
==42253== definitely lost: 128,064 bytes in 2,001 blocks
==42253== indirectly lost: 0 bytes in 0 blocks
==42253== possibly lost: 556,272 bytes in 193 blocks
==42253== still reachable: 3,995,215 bytes in 10,450 blocks
==42253== of which reachable via heuristic:
==42253== stdstring : 60,922 bytes in 1,598 blocks
==42253== suppressed: 0 bytes in 0 blocks
其中标明了确定泄漏的,可能泄漏的,以及各种堆栈信息。