利用Valgrind工具进行内存泄露检测和性能分析
Valgrind通常用来成分析程序性能及程序中的内存泄露错误
一 、Valgrind工具集简绍
Valgrind包含下列工具:
1、memcheck:检查程序中的内存问题,如泄漏、越界、非法指针等。
2、callgrind:检测程序代码的运行时间和调用过程,以及分析程序性能。
3、cachegrind:分析CPU的cache命中率、丢失率,用于进行代码优化。
4、helgrind:用于检查多线程程序的竞态条件。
5、massif:堆栈分析器,指示程序中使用了多少堆内存等信息。
6、lackey:
7、nulgrind:
这几个工具的使用是通过命令:valgrand --tool=name 程序名来分别调用的,当不指定tool参数时默认是 --tool=memcheck
二、 Valgrind工具详解
1.Memcheck
最常用的工具,用来检测程序中出现的内存问题,所有对内存的读写都会被检测到,一切对malloc、free、new、delete的调用都会被捕获。所以,它能检测以下问题:
1、对未初始化内存的使用;
2、读/写释放后的内存块;
3、读/写超出malloc分配的内存块;
4、读/写不适当的栈中内存块;
5、内存泄漏,指向一块内存的指针永远丢失;
6、不正确的malloc/free或new/delete匹配;
7、memcpy()相关函数中的dst和src指针重叠。
三、Valgrind 安装
1.依赖 autoconf、automake,(autoconf)安装示例如下:
autoconf 安装源:wget http://ftp.gnu.org/gnu/automake/automake-1.14.tar.gz
automake 安装源: wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
(安装顺序没有先后之别)
方法一:
wget http://ftp.gnu.org/gnu/automake/automake-1.14.tar.gz
tar -zxvf automake-1.14.tar.gz
cd automake-1.14
./bootstrap.sh
./configure
make && make install;
automake --version
方法二:
yum install -y autoconf
2. 安装Valgrind
wget http://valgrind.org/downloads/valgrind-3.10.1.tar.bz2
valgrind-3.10.1
./autogen.sh
./configure
make && install
四、示例
运行命令: valgrind --leak-check=full ./编译后的程序名称
eg:valgrind --leak-check=full ./valtest
#include<stdio.h> #include<stdlib.h> #include<string.h> void test(){ int *ptr = malloc(sizeof(int)*10); //length =10 ptr[10] = 7; //这个长度是11 的整型数组 地址不存在 内存越界 memcpy(ptr+1,ptr,5);//内存重叠 free(ptr); free(ptr);//重复释放 int *ptr1; ptr1 = 1; //非法指针 } int main(int argc,char **argv){ test(); return 0; }
==28146== Memcheck, a memory error detector ==28146== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. ==28146== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info ==28146== Command: ./valtest ==28146== ==28146== Invalid write of size 4 //4个字节无效写入 也就是内存越界 找不到写进去 ==28146== at 0x4005FE: test (in /home/learn/c/valgrind-3.10.1/valtest) ==28146== by 0x40065A: main (in /home/learn/c/valgrind-3.10.1/valtest) ==28146== Address 0x51f5068 is 0 bytes after a block of size 40 alloc'd ==28146== at 0x4C28BBD: malloc (vg_replace_malloc.c:296) ==28146== by 0x4005F1: test (in /home/learn/c/valgrind-3.10.1/valtest) ==28146== by 0x40065A: main (in /home/learn/c/valgrind-3.10.1/valtest) ==28146== ==28146== Source and destination overlap in memcpy(0x51f5044, 0x51f5040, 5) //使用memcpy函数 数据源和目标源重叠 ==28146== at 0x4C2D19C: memcpy@@GLIBC_2.14 (vg_replace_strmem.c:915) ==28146== by 0x40061F: test (in /home/learn/c/valgrind-3.10.1/valtest) ==28146== by 0x40065A: main (in /home/learn/c/valgrind-3.10.1/valtest) ==28146== ==28146== Invalid free() / delete / delete[] / realloc() //重复释放 ==28146== at 0x4C29CD7: free (vg_replace_malloc.c:473) ==28146== by 0x400637: test (in /home/learn/c/valgrind-3.10.1/valtest) ==28146== by 0x40065A: main (in /home/learn/c/valgrind-3.10.1/valtest) ==28146== Address 0x51f5040 is 0 bytes inside a block of size 40 free'd ==28146== at 0x4C29CD7: free (vg_replace_malloc.c:473) ==28146== by 0x40062B: test (in /home/learn/c/valgrind-3.10.1/valtest) ==28146== by 0x40065A: main (in /home/learn/c/valgrind-3.10.1/valtest) ==28146== ==28146== ==28146== HEAP SUMMARY: ==28146== in use at exit: 0 bytes in 0 blocks ==28146== total heap usage: 1 allocs, 2 frees, 40 bytes allocated ==28146== ==28146== All heap blocks were freed -- no leaks are possible ==28146== ==28146== For counts of detected and suppressed errors, rerun with: -v ==28146== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 6 from 6)