Unix下C程序内存泄漏检测工具Valgrind安装与使用
来源:互联网 发布:办公审批软件 编辑:程序博客网 时间:2024/05/18 03:01
Valgrind是一款用于内存调试、内存泄漏检测以及性能分析的软件开发工具。
Valgrind的最初作者是Julian Seward,他于2006年由于在开发Valgrind上的工作获得了第二届Google-O'Reilly开源代码奖。
Valgrind遵守GNU通用公共许可证条款,是一款自由软件。
官网
http://www.valgrind.org
下载与安装
#wget http://www.valgrind.org/downloads/valgrind-3.8.1.tar.bz2
#tar xvf valgrind-3.8.1.tar.bz2
#cd valgrind-3.8.1
#./configure --prefix=/usr/local/webserver/valgrind
#make
#make install
测试代码
- #include <stdlib.h>
- int* func(void)
- {
- int* x = malloc(10 * sizeof(int));
- x[10] = 0; //问题1: 数组下标越界
- }
- int main(void)
- {
- int* x=NULL;
- x=func();
- //free(x);
- x=NULL;
- return 0; //问题2: 内存没有释放
- }
编译
#gcc -g -o test test.c
内存检查
#valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./test
报告:
说明
Invalid write of size 4:表示数组越界写了4字节
40 bytes in 1 blocks:表示因程序退出而发生内存泄露40字节
修复bug,重新检查提示已经没有内存泄露
文档:
Valgrind 中包含的 Memcheck 工具可以检查以下的程序错误:
使用未初始化的内存 (Use of uninitialised memory)
使用已经释放了的内存 (Reading/writing memory after it has been free’d)
使用超过malloc分配的内存空间(Reading/writing off the end of malloc’d blocks)
对堆栈的非法访问 (Reading/writing inappropriate areas on the stack)
申请的空间是否有释放 (Memory leaks – where pointers to malloc’d blocks are lost forever)
malloc/free/new/delete申请和释放内存的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete [])
src和dst的重叠(Overlapping src and dst pointers in memcpy() and related functions)
重复free
其他参考工具:likwid http://code.google.com/p/likwid/downloads/list
- Unix下C程序内存泄漏检测工具Valgrind安装与使用
- Unix下C程序内存泄漏检测工具Valgrind安装与使用
- Unix下C程序内存泄漏检测工具Valgrind安装与使用
- Unix下C程序内存泄漏检测工具Valgrind安装与使用
- Unix下C程序内存泄漏检测工具Valgrind安装与使用
- Unix下C程序内存泄漏检测工具Valgrind安装与使用
- Unix下C程序内存泄漏检测工具Valgrind安装与使用
- Unix下C程序内存泄漏检测工具Valgrind安装与使用
- Unix下C程序内存泄漏检测工具Valgrind安装与使用
- Unix/Linux下C程序内存泄露检测工具Valgrind的安装和使用
- valgrind 内存泄漏检测工具使用
- Valgrind linux程序内存泄漏检测工具
- Linux C/C++ 内存泄漏检测工具:Valgrind
- Linux C/C++ 内存泄漏检测工具Valgrind
- Linux C/C++ 内存泄漏检测工具:Valgrind
- Linux C/C++ 内存泄漏检测工具:Valgrind
- Linux C/C++ 内存泄漏检测工具:Valgrind
- Linux C/C++ 内存泄漏检测工具:Valgrind
- 分解滤波组和综合滤波组
- 利用electron-packager打包exe程序
- 【BZOJ 1055】【HAOI 2008】玩具取名【区间动规】
- 笔记本无线网黄色感叹号
- 解决win10以太网没有有效的ip配置
- Unix下C程序内存泄漏检测工具Valgrind安装与使用
- Linux下安装Eclipse及其插件
- PHP判断访问设备是否为手机端的方法
- iOS动画(Core Animation)总结
- 离散数学笔记一
- OpenCV中的HOG+SVM物体分类
- Android多渠道批量打包
- 为什么要学习angularjs2
- 守护进程