Valgrind是一款用于内存调试、内存泄漏检测以及性能分析的软件开发工具。
Valgrind遵守GNU通用公共许可证条款,是一款自由软件。
到3.3.0版本为止,Valgrind支持x86、x86-64以及PowerPC上的Linux。除此之外,还有一些其它非正式支持的类Unix平台(如FreeBSD、NetBSD以及Mac OS X)。
wget http://valgrind.org/downloads/valgrind-3.11.0.tar.bz2
tar -xjvf valgrind-3.11.0.tar.bz2
cd valgrind-3.11.0/
./configure --help来查看可以使用哪些参数设置
./configure --prefix=/usr/local/valgrind 设置路径
make 编译 -j n 多核编译
make install 安装
valgrind --leak-check=full --show-leak-kinds=all --log-file=out.log ./FGen (example)
--leak-check=full 信息显示具体泄漏位置
--log-file=out.log 将检测信息输入到日志out.log中
--show-leak-kinds=all 显示所有内存泄漏消息
C++ 代码:
#include
int main()
{
((char*)malloc(10))[10] = 100;
return 0;
}
valgrind --track-fds=yes --leak-check=full --undef-value-errors=yes ./test 命令执行
==4842== Memcheck, a memory error detector
==4842== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==4842== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==4842== Command: ./test
==4842==
==4842== Invalid write of size 1 # 这里检测到了内存越界访问
==4842== at 0x400548: main (in /home/o/software/test)
==4842== Address 0x520204a is 0 bytes after a block of size 10 alloc'd
==4842== at 0x4C2BC50: malloc (vg_replace_malloc.c:299)
==4842== by 0x400543: main (in /home/o/software/test)
==4842==
==4842==
==4842== FILE DESCRIPTORS: 3 open at exit. #打开了三个文件描述符(标准输入输出错误)
==4842== Open file descriptor 2: /dev/pts/12
==4842==
==4842==
==4842== Open file descriptor 1: /dev/pts/12
==4842==
==4842==
==4842== Open file descriptor 0: /dev/pts/12
==4842==
==4842==
==4842==
==4842== HEAP SUMMARY: #堆使用摘要
==4842== in use at exit: 10 bytes in 1 blocks
==4842== total heap usage: 1 allocs, 0 frees, 10 bytes allocated #申请/释放详情
==4842==
==4842== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1
==4842== at 0x4C2BC50: malloc (vg_replace_malloc.c:299)
==4842== by 0x400543: main (in /home/o/software/test)
==4842==
==4842== LEAK SUMMARY: 泄露摘要
==4842== definitely lost: 10 bytes in 1 blocks
==4842== indirectly lost: 0 bytes in 0 blocks
==4842== possibly lost: 0 bytes in 0 blocks
==4842== still reachable: 0 bytes in 0 blocks
==4842== suppressed: 0 bytes in 0 blocks
==4842==
==4842== For counts of detected and suppressed errors, rerun with: -v
==4842== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 1 from 1)
可以看出上面检测到了内存越界访问和内存泄露
错误类型说明:
definitely lost :
no pointer to the block can be found memcheck won't report such blocks individually unless –show-reachable=yes is specified 确定的内存泄露,已经不能够访问这块内存
indirectly lost :
the block is lost, not because there are no pointers to it, but rather because all the blocks that point to
it are themselves lost 指向该内存的指针都位于内存泄露处
possibly lost:
a chain of one or more pointers to the block has been found, but at least one of the pointers is an interior-pointer
可能的内存泄露,仍然存在某个指针能够访问某快内存,但该指针指向的已经不是该内存首位置
still reachable:
A start-pointer or chain of start-pointers to the block is found.memcheck won’t report unless –show-reachable=yes is specified 内存指针还在还有机会使用或者释放,指针指向的动态内存还没有被释放就退出了
最常用的工具,用来检测程序中出现的内存问题,所有对内存的读写都会被检测到,一切对malloc、free、new、 delete的调用都会被捕获。所以,它能检测以下问题:
1、对未初始化内存的使用;
2、读/写释放后的内存块;
3、读/写超出malloc分配的内存块;
4、读/写不适当的栈中内存块;
5、内存泄漏,指向一块内存的指针永远丢失;
6、不正确的malloc/free或new/delete匹配;
7、memcpy()相关函数中的dst和src指针重叠。
这些问题往往是C/C++程序员最头疼的问题,Memcheck能在这里帮上大忙
和gprof类似的分析工具,但它对程序的运行观察更是入微,能给我们提供更多的信息。和gprof不同,它不需要在编译源代码时附加特殊选项,但加上调试选项是推荐的。Callgrind收集程序运行时的一些数据,建立函数调用关系图,还可以有选择地进行cache模拟。在运行结束时,它会把分析数据写入一个文件。callgrind_annotate可以把这个文件的内容转化成可读的形式
Cache分析器,它模拟CPU中的一级缓存I1,Dl和二级缓存,能够精确地指出程序中cache的丢失和命中。如果需要,它还能够为我们提供cache丢失次数,内存引用次数,以及每行代码,每个函数,每个模块,整个程序产生的指令数。这对优化程序有很大的帮助
它主要用来检查多线程程序中出现的竞争问题。Helgrind寻找内存中被多个线程访问,而又没有一贯加锁的区域,这些区域往往是线程之间失去同步的地方,而且会导致难以发掘的错误。Helgrind实现了名为“Eraser”的竞争检测算法,并做了进一步改进,减少了报告错误的次数。不过,Helgrind仍然处于实验阶段
堆栈分析器,它能测量程序在堆栈中使用了多少内存,告诉我们堆块,堆管理块和栈的大小。Massif能帮助我们减少内存的使用,在带有虚拟内存的现代系统中,它还能够加速我们程序的运行,减少程序停留在交换区中的几率。
Massif对内存的分配和释放做profile。程序开发者通过它可以深入了解程序的内存使用行为,从而对内存使用进行优化。这个功能对C++尤其有用,因为C++有很多隐藏的内存分配和释放