valgrind工具的使用,检测内存泄露
来源:互联网 发布:pptv网络电视无法播放 编辑:程序博客网 时间:2024/05/22 07:03
第1章valgrind的安装
1.1 valgrind的介绍
Valgrind是一款用于内存调试、内存泄漏检测以及性能分析的软件开发工具。Valgrind这个名字取自北欧神话中英灵殿的入口。
Valgrind的最初作者是Julian Seward,他于2006年由于在开发Valgrind上的工作获得了第二届Google-O'Reilly开源代码奖。
Valgrind遵守GNU通用公共许可证条款,是一款自由软件。
到3.6.1版本为止,Valgrind支持{x86,amd64,arm,ppc32,ppc64}-linuxand {x86,amd64}-darwin (Mac OS X).
1.2 valgrind的安装
下载压缩包后解压、安装
[root@localhost~]#tar–jxvf valgrind-3.2.3.tar.bz2
[root@localhost ~]# cd valgrind-3.3.1
[root@localhost valgrind-3.3.1]# ./configure && make && makeinstall
第二章valgrind的使用
2.1 用法
valgrind [options] prog-and-args [options]:常用选项,适用于所有Valgrind工具
-tool=<name> 最常用的选项。运行 valgrind中名为toolname的工具。默认memcheck。
h –help 显示帮助信息。
-version 显示valgrind内核的版本,每个工具都有各自的版本。
q –quiet 安静地运行,只打印错误信息。
v –verbose 更详细的信息, 增加错误数统计。
-trace-children=no|yes 跟踪子线程? [no]
-track-fds=no|yes 跟踪打开的文件描述?[no]
-time-stamp=no|yes 增加时间戳到LOG信息? [no]
-log-fd=<number> 输出LOG到描述符文件 [2=stderr]
-log-file=<file> 将输出的信息写入到filename.PID的文件里,PID是运行程序的进行ID
-log-file-exactly=<file> 输出LOG信息到 file
-log-file-qualifier=<VAR> 取得环境变量的值来做为输出信息的文件名。 [none]
-log-socket=ipaddr:port 输出LOG到socket ,ipaddr:port
LOG信息输出
-xml=yes 将信息以xml格式输出,只有memcheck可用
-num-callers=<number> show<number> callers in stack traces [12]
-error-limit=no|yes 如果太多错误,则停止显示新错误? [yes]
-error-exitcode=<number> 如果发现错误则返回错误代码 [0=disable]
-db-attach=no|yes 当出现错误,valgrind会自动启动调试器gdb。[no]
-db-command=<command> 启动调试器的命令行选项[gdb -nw %f %p]
适用于Memcheck工具的相关选项:
-leak-check=no|summary|full 要求对leak给出详细信息? [summary]
-leak-resolution=low|med|high how much btmerging in leak check [low]
-show-reachable=no|yes show reachableblocks in leak check? [no]
2.2 Memcheck工具功能
使用未初始化的内存 (Useof uninitialised memory)
使用已经释放了的内存(Reading/writing memory after it has been free’d)
使用超过 malloc分配的内存空间(Reading/writing off the endof malloc’d blocks)
对堆栈的非法访问(Reading/writing inappropriate areas on the stack)
申请的空间是否有释放(Memory leaks – wherepointers to malloc’d blocks are lost
forever)
malloc/free/new/delete申请和释放内存的匹配(Mismatched use ofmalloc/new/new [] vs
free/delete/delete [])
src和dst的重叠(Overlappingsrc and dst pointers in memcpy() and related functions)
2.3 示例(测试一个C)
#include <stdio.h> #include <stdlib.h>int main(){ struct mm{ int a; int b; }kk,*p; kk.a = 10; printf("kk.a : %d \n",kk.a); p = (struct mm *)malloc(sizeof(struct mm)); p->a = 9; printf("p->a : %d \n",p->a); return 0;}
操作如下:
[root@localhost ~]# vim hello.c
[root@localhost ~]# gcc -g -o hello hello.c
[root@localhost ~]# ./hello
结果如下:
然后用valgrind命令:
[root@localhost ~]# valgrind --tool=memcheck--leak-check=yes --show-reachable=yes ./hello
可以看出上面提示“malloc/free:1 allocs, 0 frees, 8 bytes allocated.”,“definitely
lost: 8 bytes in 1 blocks.”。即丢失了8个字节。
- valgrind工具的使用,检测内存泄露
- 使用valgrind检测内存泄露的问题。
- valgrind 内存泄露检测
- valgrind检测内存泄露
- 使用dmalloc&valgrind检测内存泄露
- linux使用valgrind 工具检查内存泄露
- Linux下使用valgrind工具对C++编程检测内存泄露
- Valgrind 介绍-Valgrind 检测c++内存泄露
- (zt)使用Valgrind检测linux上c++内存泄露
- Ubuntu下使用valgrind检测内存泄露流程
- 使用valgrind检测ATS插件中的内存泄露
- valgrind 内存泄露 监测工具
- 检测内存泄露的工具
- Linux下的内存泄露检测(valgrind)
- linux下检测 内存泄露的利器 valgrind
- 使用Valgrind 查找内存泄露
- 使用Valgrind 查找内存泄露
- 使用valgrind检查内存泄露
- UIImage的合并和裁剪
- gdb调试经验
- python 打zip 包
- libevent和libev
- Boyer-Moore algorithm
- valgrind工具的使用,检测内存泄露
- 第二十天【java虐我千百遍,我待java如初恋】
- Eclipse使用技巧总结(四)——代码重构专题
- Linux下svn常用命令
- Web服务器性能测试介绍
- Linux下如何生成core dump文件
- Horspool algorithm
- Sunday algorithm
- 网络编程:Reactor和Proactor模式