`

valgrind工具 内存泄露检查及性能分析

阅读更多

转自:http://www.linuxidc.com/Linux/2012-06/63754.htm

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指针重叠。

这些问题往往是C/C++程序员最头疼的问题,Memcheck能在这里帮上大忙。
例如:

  1. #include <stdlib.h>  
  2. #include <malloc.h>  
  3. #include <string.h>  
  4.   
  5. void test()  
  6. {  
  7.     int *ptr = malloc(sizeof(int)*10);  
  8.   
  9.     ptr[10] = 7; // 内存越界  
  10.   
  11.     memcpy(ptr +1, ptr, 5); // 踩内存  
  12.   
  13.   
  14.     free(ptr);   
  15.     free(ptr);// 重复释放  
  16.   
  17.     int *p1;  
  18.     *p1 = 1; // 非法指针  
  19. }  
  20.   
  21. int main(void)  
  22. {  
  23.     test();  
  24.     return 0;  
  25. }  

将程序编译生成可执行文件后执行:valgrind --leak-check=full ./程序名

 

输出结果如下:

  1. ==4832== Memcheck, a memory error detector  
  2. ==4832== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.  
  3. ==4832== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info  
  4. ==4832== Command: ./tmp  
  5. ==4832==   
  6. ==4832== Invalid write of size 4      // 内存越界  
  7. ==4832==    at 0x804843F: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  8. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  9. ==4832==  Address 0x41a6050 is 0 bytes after a block of size 40 alloc'd  
  10. ==4832==    at 0x4026864: malloc (vg_replace_malloc.c:236)  
  11. ==4832==    by 0x8048435: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  12. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  13. ==4832==   
  14. ==4832== Source and destination overlap in memcpy(0x41a602c, 0x41a6028, 5) // 踩内存  
  15. ==4832==    at 0x4027BD6: memcpy (mc_replace_strmem.c:635)  
  16. ==4832==    by 0x8048461: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  17. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  18. ==4832==   
  19. ==4832== Invalid free() / delete / delete[] // 重复释放  
  20. ==4832==    at 0x4025BF0: free (vg_replace_malloc.c:366)  
  21. ==4832==    by 0x8048477: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  22. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  23. ==4832==  Address 0x41a6028 is 0 bytes inside a block of size 40 free'd  
  24. ==4832==    at 0x4025BF0: free (vg_replace_malloc.c:366)  
  25. ==4832==    by 0x804846C: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  26. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  27. ==4832==   
  28. ==4832== Use of uninitialised value of size 4 // 非法指针  
  29. ==4832==    at 0x804847B: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  30. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  31. ==4832==   
  32. ==4832==   
  33. ==4832== Process terminating with default action of signal 11 (SIGSEGV) //由于非法指针赋值导致的程序崩溃  
  34. ==4832==  Bad permissions for mapped region at address 0x419FFF4  
  35. ==4832==    at 0x804847B: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  36. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  37. ==4832==   
  38. ==4832== HEAP SUMMARY:  
  39. ==4832==     in use at exit: 0 bytes in 0 blocks  
  40. ==4832==   total heap usage: 1 allocs, 2 frees, 40 bytes allocated  
  41. ==4832==   
  42. ==4832== All heap blocks were freed -- no leaks are possible  
  43. ==4832==   
  44. ==4832== For counts of detected and suppressed errors, rerun with: -v  
  45. ==4832== Use --track-origins=yes to see where uninitialised values come from  
  46. ==4832== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 11 from 6)  
  47. Segmentation fault  

从valgrind的检测输出结果看,这几个错误都找了出来。

2.Callgrind

和gprof类似的分析工具,但它对程序的运行观察更是入微,能给我们提供更多的信息。和gprof不同,它不需要在编译源代码时附加特殊选项,但加上调试选项是推荐的。Callgrind收集程序运行时的一些数据,建立函数调用关系图,还可以有选择地进行cache模拟。在运行结束时,它会把分析数据写入一个文件。callgrind_annotate可以把这个文件的内容转化成可读的形式。

生成可视化的图形需要下载gprof2dot:http://http://jrfonseca.googlecode.com/svn/trunk/gprof2dot/gprof2dot.py

这是个python脚本,把它下载之后修改其权限chmod +7 gprof2dot.py ,并把这个脚本添加到$PATH路径中的任一文件夹下,我是将它放到了/usr/bin目录下,这样就可以直接在终端下执行gprof2dot.py了。

Callgrind可以生成程序性能分析的图形,首先来说说程序性能分析的工具吧,通常可以使用gnu自带的gprof,它的使用方法是:在编译程序时添加-pg参数,例如:

  1. #include <stdio.h>  
  2. #include <malloc.h>   
  3. void test()  
  4. {  
  5.     sleep(1);  
  6. }  
  7. void f()  
  8. {  
  9.     int i;  
  10.     for( i = 0; i < 5; i ++)  
  11.         test();  
  12. }  
  13. int main()  
  14. {  
  15.     f();  
  16.     printf("process is over!\n");  
  17.     return 0;  
  18. }  

首先执行 gcc -pg -o tmp tmp.c,然后运行该程序./tmp,程序运行完成后会在当前目录下生成gmon.out文件(这个文件gprof在分析程序时需要),
再执行gprof ./tmp | gprof2dot.py |dot -Tpng -o report.png,打开report.png结果:

 

 

显示test被调用了5次,程序中耗时所占百分比最多的是test函数。

再来看 Callgrind的生成调用图过程吧,执行:valgrind --tool=callgrind ./tmp,执行完成后在目录下生成"callgrind.out.XXX"的文件这是分析文件,可以直接利用:callgrind_annotate callgrind.out.XXX 打印结果,也可以使用:gprof2dot.py -f callgrind callgrind.out.XXX |dot -Tpng -o report.png 来生成图形化结果:

 

它生成的结果非常详细,甚至连函数入口,及库函数调用都标识出来了。

3.Cachegrind

       Cache分析器,它模拟CPU中的一级缓存I1,Dl和二级缓存,能够精确地指出程序中cache的丢失和命中。如果需要,它还能够为我们提供cache丢失次数,内存引用次数,以及每行代码,每个函数,每个模块,整个程序产生的指令数。这对优化程序有很大的帮助。

    作一下广告:valgrind自身利用该工具在过去几个月内使性能提高了25%-30%。据早先报道,kde的开发team也对valgrind在提高kde性能方面的帮助表示感谢。

它的使用方法也是:valgrind --tool=cachegrind 程序名,

4.Helgrind

    它主要用来检查多线程程序中出现的竞争问题。Helgrind寻找内存中被多个线程访问,而又没有一贯加锁的区域,这些区域往往是线程之间失去同步的地方,而且会导致难以发掘的错误。Helgrind实现了名为“Eraser”的竞争检测算法,并做了进一步改进,减少了报告错误的次数。不过,Helgrind仍然处于实验阶段。

首先举一个竞态的例子吧:

  1. #include <stdio.h>  
  2. #include <pthread.h>  
  3. #define NLOOP 50  
  4. int counter = 0; /* incremented by threads */  
  5. void *threadfn(void *);  
  6.   
  7. int main(int argc, char **argv)  
  8. {  
  9.     pthread_t tid1, tid2,tid3;  
  10.   
  11.   
  12.     pthread_create(&tid1, NULL, &threadfn, NULL);  
  13.     pthread_create(&tid2, NULL, &threadfn, NULL);  
  14.     pthread_create(&tid3, NULL, &threadfn, NULL);  
  15.   
  16.   
  17.     /* wait for both threads to terminate */  
  18.     pthread_join(tid1, NULL);  
  19.     pthread_join(tid2, NULL);  
  20.     pthread_join(tid3, NULL);  
  21.   
  22.   
  23.     return 0;  
  24. }  
  25.   
  26. void *threadfn(void *vptr)  
  27. {  
  28.       int i, val;  
  29.       for (i = 0; i < NLOOP; i++) {  
  30.     val = counter;  
  31.     printf("%x: %d \n", (unsigned int)pthread_self(),  val+1);  
  32.     counter = val+1;  
  33.       }  
  34.       return NULL;  
  35. }  

 

这段程序的竞态在30~32行,我们想要的效果是3个线程分别对全局变量累加50次,最后全局变量的值为150,由于这里没有加锁,很明显竞态使得程序不能达到我们的目标。我们来看Helgrind是如何帮我们检测到竞态的。先编译程序:gcc -o test thread.c -lpthread ,然后执行:valgrind --tool=helgrind ./test 输出结果如下:

49c0b70: 1 
49c0b70: 2 
==4666== Thread #3 was created
==4666==    at 0x412E9D8: clone (clone.S:111)
==4666==    by 0x40494B5: pthread_create@@GLIBC_2.1 (createthread.c:256)
==4666==    by 0x4026E2D: pthread_create_WRK (hg_intercepts.c:257)
==4666==    by 0x4026F8B: pthread_create@* (hg_intercepts.c:288)
==4666==    by 0x8048524: main (in /home/yanghao/Desktop/testC/testmem/a.out)
==4666== 
==4666== Thread #2 was created
==4666==    at 0x412E9D8: clone (clone.S:111)
==4666==    by 0x40494B5: pthread_create@@GLIBC_2.1 (createthread.c:256)
==4666==    by 0x4026E2D: pthread_create_WRK (hg_intercepts.c:257)
==4666==    by 0x4026F8B: pthread_create@* (hg_intercepts.c:288)
==4666==    by 0x8048500: main (in /home/yanghao/Desktop/testC/testmem/a.out)
==4666== 
==4666== Possible data race during read of size 4 at 0x804a028 by thread #3
==4666==    at 0x804859C: threadfn (in /home/yanghao/Desktop/testC/testmem/a.out)
==4666==    by 0x4026F60: mythread_wrapper (hg_intercepts.c:221)
==4666==    by 0x4048E98: start_thread (pthread_create.c:304)
==4666==    by 0x412E9ED: clone (clone.S:130)
==4666==  This conflicts with a previous write of size 4 by thread #2
==4666==    at 0x80485CA: threadfn (in /home/yanghao/Desktop/testC/testmem/a.out)
==4666==    by 0x4026F60: mythread_wrapper (hg_intercepts.c:221)
==4666==    by 0x4048E98: start_thread (pthread_create.c:304)
==4666==    by 0x412E9ED: clone (clone.S:130)
==4666== 
==4666== Possible data race during write of size 4 at 0x804a028 by thread #2
==4666==    at 0x80485CA: threadfn (in /home/yanghao/Desktop/testC/testmem/a.out)
==4666==    by 0x4026F60: mythread_wrapper (hg_intercepts.c:221)
==4666==    by 0x4048E98: start_thread (pthread_create.c:304)
==4666==    by 0x412E9ED: clone (clone.S:130)
==4666==  This conflicts with a previous read of size 4 by thread #3
==4666==    at 0x804859C: threadfn (in /home/yanghao/Desktop/testC/testmem/a.out)
==4666==    by 0x4026F60: mythread_wrapper (hg_intercepts.c:221)
==4666==    by 0x4048E98: start_thread (pthread_create.c:304)
==4666==    by 0x412E9ED: clone (clone.S:130)
==4666== 
49c0b70: 3 
......
55c1b70: 51 
==4666== 
==4666== For counts of detected and suppressed errors, rerun with: -v
==4666== Use --history-level=approx or =none to gain increased speed, at
==4666== the cost of reduced accuracy of conflicting-access information
==4666== ERROR SUMMARY: 8 errors from 2 contexts (suppressed: 99 from 31)

helgrind成功的找到了竞态的所在位置,标红所示。

 

5. Massif

    堆栈分析器,它能测量程序在堆栈中使用了多少内存,告诉我们堆块,堆管理块和栈的大小。Massif能帮助我们减少内存的使用,在带有虚拟内存的现代系统中,它还能够加速我们程序的运行,减少程序停留在交换区中的几率。

       Massif对内存的分配和释放做profile。程序开发者通过它可以深入了解程序的内存使用行为,从而对内存使用进行优化。这个功能对C++尤其有用,因为C++有很多隐藏的内存分配和释放。

 

此外,lackey和nulgrind也会提供。Lackey是小型工具,很少用到;Nulgrind只是为开发者展示如何创建一个工具。我们就不做介绍了。

三 使用Valgrind

       Valgrind使用起来非常简单,你甚至不需要重新编译你的程序就可以用它。当然如果要达到最好的效果,获得最准确的信息,还是需要按要求重新编译一下的。比如在使用memcheck的时候,最好关闭优化选项。

       valgrind命令的格式如下:

       valgrind [valgrind-options] your-prog [your-prog options]

一些常用的选项如下:

选项

作用

-h --help

显示帮助信息。

--version

显示valgrind内核的版本,每个工具都有各自的版本。

-q --quiet

安静地运行,只打印错误信息。

-v --verbose

打印更详细的信息。

--tool=<toolname> [default: memcheck]

最常用的选项。运行valgrind中名为toolname的工具。如果省略工具名,默认运行memcheck。

--db-attach=<yes|no> [default: no]

绑定到调试器上,便于调试错误。

分享到:
评论

相关推荐

    Valgrind内存调试、内存泄漏检测以及性能分析工具

    Valgrind是一款用于内存调试、内存泄漏检测以及性能分析的软件开发工具。Valgrind这个名字取自北欧神话中英灵殿的入口。  Valgrind的最初作者是Julian Seward,他于2006年由于在开发Valgrind上的工作获得了第二届...

    arm环境内存泄漏检测工具valgrind

    "arm环境内存泄漏检测工具valgrind"就是这样一个针对ARM平台的专业工具,它能帮助我们识别并解决内存泄漏问题。 Valgrind是一个开源的动态分析工具集,主要用于调试、性能评估和内存错误检测。在ARM环境下,...

    Valgrind3.6.0一款用于内存调试、内存泄漏检测以及性能分析的软件开发工具

    Valgrind是一款强大的开源软件开发工具,主要用于内存调试、内存泄漏检测和性能分析。它在Linux环境下尤为常用,为开发者提供了深入的程序行为洞察,帮助找出潜在的问题,从而提高软件的质量和稳定性。Valgrind ...

    Linux 系统中valgrind检查内存泄露

    为了解决这个问题,Linux提供了一个强大的工具——valgrind,它是一款开源的内存调试、内存泄漏检测和性能分析工具。 标题中的"Linux系统中valgrind检查内存泄露"意味着我们要讨论如何利用valgrind来发现和定位程序...

    linux平台下内存泄漏检测工具valgrind3.11

    `--leak-check=yes`选项告诉Valgrind开启详细的内存泄漏检查。 3. **分析报告**:Valgrind会在程序执行结束后提供详细的报告,指出哪些内存块没有被正确释放,以及分配这些内存的源代码位置。这对于定位问题非常有...

    valgrind for NDK (ANDROID NDK内存检测工具)

    `Valgrind` 是一个开源的内存错误检测和性能分析工具,它能够帮助开发者找出程序中的内存泄漏、未初始化的内存访问、无效的指针使用等问题。`Valgrind for NDK` 是将 `valgrind` 工具移植到Android环境,特别是针对...

    valgrind内存检查工具

    例如,`valgrind --tool=memcheck --leak-check=yes your_program`将启动Memcheck工具,并开启内存泄漏检查。 3. **分析结果**:Valgrind会生成详细的报告,列出可能存在的内存问题。报告包括内存泄漏的位置、分配...

    linux下检查内存泄漏的工具+例子

    Valgrind的`memcheck`工具专门用于内存错误检测,`--leak-check=yes`则表示开启内存泄漏检查。 为了更好地理解Valgrind的用法,你可以参考提供的`demo`测试程序。这个演示程序可能会包含一些故意的内存泄漏,运行时...

    valgrind内存检测工具

    Valgrind是一款强大的内存调试、性能分析和内存泄漏检测工具,尤其在Linux环境下,它被广泛用于C++程序的开发和优化。Valgrind通过构建一个虚拟机来运行你的程序,从而可以在程序运行时进行细致的监控,帮助开发者...

    内存泄露检测工具及学习资料(valgrind)

    内存泄露是程序设计中的常见问题,特别是在长时间运行的服务或应用中,它会导致系统资源逐渐耗尽,最终影响程序性能甚至导致系统崩溃。Valgrind是一款强大的动态分析工具,专用于帮助开发者检测和解决Linux环境下的...

    C++内存泄露检测器

    比如Valgrind,这是一个强大的内存错误检测工具,其中的Memcheck子工具专门用于检测内存泄漏。它会记录每个内存块的分配和释放,如果程序结束时仍有未释放的内存,Valgrind就会报告这些内存泄漏。 3. **智能指针**...

    内存泄露检测工具

    17. Electric Software GlowCode:是一个完整的错误诊断和运行时性能分析工具包,用于检测 C++ 和 .NET 代码中的内存泄漏问题。 Electric Software GlowCode 工具可以检测内存泄漏问题,并提供了详细的错误信息,...

    内存泄露检查工具

    本文将详细介绍如何使用内存泄露检查工具来帮助开发者检测并修复这些问题。 内存泄露发生时,程序分配的内存没有被正确释放,随着时间推移,未释放的内存积累,最终可能导致系统资源耗尽。为了解决这个问题,有多种...

    valgrind及graphviz分析c++性能瓶颈

    * Valgrind 可以检测到内存泄漏、缓存未命中、缓存命中率等性能问题。 * Valgrind 可以生成详细的性能分析报告,帮助开发者了解程序的性能瓶颈。 使用 Graphviz 可视化性能分析结果的优点: * Graphviz 可以将性能...

    两个超棒的内存泄露检测工具

    Valgrind是一款开源的动态分析工具,广泛用于内存泄漏检测、性能分析以及错误检测。它通过在运行时为程序创建一个虚拟机,从而能够监控程序的内存操作。Valgrind提供了多个子工具,其中Memcheck是最著名的内存检测...

    vc++内存泄漏检测工具

    内存泄漏检测工具除了"leakdiag125"之外,还有其他知名的工具,如Valgrind(主要用于Linux环境)、Visual Leak Detector (VLD)、Dr. Memory等,它们各有特点和适用场景,开发者可以根据具体需求选择合适的工具。 ...

    linux内存泄露检测工具

    检测内存泄露可以使用 Valgrind 工具,它可以检测到代码中的内存泄露问题。 Valgrind 是一个功能强大且灵活的内存检测工具,它可以检测到代码中的内存泄露、缓存未命中、竞争数据等问题。Valgrind 包含三个标准工具...

    C++中内存泄露检测工具

    1. Valgrind:Valgrind是一款强大的动态分析工具,它可以检测内存泄露、无效内存访问和其他内存错误。它通过模拟CPU指令来运行程序,从而可以详细追踪内存操作。 2. Leaks:Leak是Apple开发的一款内存检测工具,...

    C++内存泄漏检测工具

    内存泄漏检测工具有多种,除了Virtual Leak Detector,还有Valgrind、LeakSanitizer等。它们各有优缺点,选择哪一款取决于项目需求、平台兼容性以及开发者个人喜好。例如,Valgrind虽然功能强大,但可能会影响程序的...

Global site tag (gtag.js) - Google Analytics