Linux c++上常用内存泄露检测工具有valgrind, Rational purify。Valgrind免费。Valgrind 可以在 32 位或 64 位 PowerPC/Linux 内核上工作。
Valgrind工具包包含多个工具,如Memcheck,Cachegrind,Helgrind, Callgrind,Massif。下面分别介绍个工具的作用:
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)
Valgrind不检查静态分配数组的使用情况。
Valgrind占用了更多的内存--可达两倍于你程序的正常使用量。如果你用Valgrind来检测使用大量内存的程序就会遇到问题,它可能会用很长的 时间来运行测试
2.1. 下载安装
http://www.valgrind.org
安装
./configure;make;make install
2.2. 编译程序
被检测程序加入 –g -fno-inline 编译选项保留调试信息。
2.3. 内存泄露检测
$ valgrind --leak-check=full --show-reachable=yes --trace-children=yes ./iquery -f ../conf/se.conf_forum -t ~/eragon/forum_thread_data/f.log -NT -cache 0
其中--leak-check=full 指的是完全检查内存泄漏,--show-reachable=yes是显示内存泄漏的地点,--trace-children=yes是跟入子进程。当程 序正常退出的时候valgrind自然会输出内存泄漏的信息。
==4591==
==4591== Thread 1:
==4591== Conditional jump or move depends on uninitialised value(s)
==4591== at 0x805687B: main (TestQuery.cpp:478)
==4591==
==4591== Conditional jump or move depends on uninitialised value(s)
==4591== at 0x8056894: main (TestQuery.cpp:478)
==4591==
==4591== Conditional jump or move depends on uninitialised value(s)
==4591== at 0x80568AD: main (TestQuery.cpp:478)
==4591== Warning: set address range perms: large range 215212032 (noaccess)
==4591== Warning: set address range perms: large range 125145088 (noaccess)
==4591==
==4591== ERROR SUMMARY: 6 errors from 4 contexts (suppressed: 18 from 1)
==4591== malloc/free: in use at exit: 496 bytes in 2 blocks.
==4591== malloc/free: 928,605 allocs, 928,603 frees, 2,514,165,074 bytes allocated.
==4591== For counts of detected errors, rerun with: -v
==4591== searching for pointers to 2 not-freed blocks.
==4591== checked 10,260,564 bytes.
==4591==
==4591==
==4591== 144 bytes in 1 blocks are possibly lost in loss record 1 of 2
==4591== at 0x4005906: calloc (vg_replace_malloc.c:279)
==4591== by 0xB3671A: _dl_allocate_tls (in /lib/ld-2.3.4.so)
==4591== by 0xD9491E: pthread_create@@GLIBC_2.1 (in /lib/tls/libpthread-2.3.4.so)
==4591== by 0x8200C66: public_unit::CThread::start(void*) (Thread.cpp:25)
==4591== by 0x80567C3: main (TestQuery.cpp:473)
==4591==
==4591==
==4591== 352 bytes in 1 blocks are still reachable in loss record 2 of 2
==4591== at 0x40044F6: malloc (vg_replace_malloc.c:149)
==4591== by 0xB9905E: __fopen_internal (in /lib/tls/libc-2.3.4.so)
==4591== by 0xB9911C: fopen@@GLIBC_2.1 (in /lib/tls/libc-2.3.4.so)
==4591== by 0x805940C: CSearchThread::run(void*) (TestQuery.cpp:363)
==4591== by 0x8200D09: public_unit::CThread::thread_func(void*) (Thread.cpp:44)
==4591== by 0xD94370: start_thread (in /lib/tls/libpthread-2.3.4.so)
==4591== by 0xC0DFFD: clone (in /lib/tls/libc-2.3.4.so)
==4591==
==4591== LEAK SUMMARY:
==4591== definitely lost: 0 bytes in 0 blocks.
==4591== possibly lost: 144 bytes in 1 blocks.
==4591== still reachable: 352 bytes in 1 blocks.
==4591== suppressed: 0 bytes in 0 blocks.
关键字在:ERROR SUMMARY, LEAK SUMMARY
"definitely lost" means your program is leaking memory -- fix it!
"possibly lost" means your program is probably leaking memory, unless you're doing funny things with pointers.
"still reachable" means your program is probably ok -- it didn't free some memory it could have. This is quite common and often reasonable. Don't use --show-reachable=yes if you don't want to see these reports.
"suppressed" means that a leak error has been suppressed. There are some suppressions in the default suppression files. You can ignore suppressed errors
另外一种方式,激活加载调试器
gcc -Wall -g -pg -o get_XMLDOC get_XMLDOC.c
$ valgrind --db-attach=yes --leak-check=full ./get_XMLDOC ~/eragon/data/offer_gb.xml 1.xml 10
==8956== Memcheck, a memory error detector.
==8956== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
==8956== Using LibVEX rev 1606, a library for dynamic binary translation.
==8956== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
==8956== Using valgrind-3.2.0, a dynamic binary instrumentation framework.
==8956== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
==8956== For more details, rerun with: -v
==8956==
==8956==
==8956== ---- Attach to debugger ? --- [Return/N/n/Y/y/C/c] ----
==8956==
==8956== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 12 from 1)
==8956== malloc/free: in use at exit: 1,953 bytes in 2 blocks.
==8956== malloc/free: 4 allocs, 2 frees, 2,657 bytes allocated.
==8956== For counts of detected errors, rerun with: -v
==8956== searching for pointers to 2 not-freed blocks.
==8956== checked 52,840 bytes.
==8956==
==8956== 1 bytes in 1 blocks are definitely lost in loss record 1 of 2
==8956== at 0x40044F6: malloc (vg_replace_malloc.c:149)
==8956== by 0x80488C0: main (get_XMLDOC.c:38)
==8956==
==8956== LEAK SUMMARY:
==8956== definitely lost: 1 bytes in 1 blocks.
==8956== possibly lost: 0 bytes in 0 blocks.
==8956== still reachable: 1,952 bytes in 1 blocks.
==8956== suppressed: 0 bytes in 0 blocks.
==8956== Reachable blocks (those to which a pointer was found) are not shown.
==8956== To see them, rerun with: --show-reachable=yes
Profiling timer expired
2.4. 检查性能瓶颈
$valgrind --tool=callgrind ./iquery -f ../conf/se.conf_forum -s "forum_thread?q=mp4"
…
==4607==
==4607== Events : Ir
==4607== Collected : 251772397
==4607==
==4607== I refs: 251,772,397
4607为进程号。
$ ll
-rw------- 1 search search 712159 7月 9 22:31 callgrind.out.4607
$ callgrind_annotate --auto=yes callgrind.out.4607
WARNING: header line 2 malformed, ignoring
line: 'creator: callgrind-3.2.0'
--------------------------------------------------------------------------------
I1 cache:
D1 cache:
L2 cache:
Timerange: Basic block 0 - 46942078
Trigger: Program termination
Profiled target: ./iquery -f ../conf/se.conf_forum -s forum_thread?q=mp4 (PID 4607, part 1)
Events recorded: Ir
Events shown: Ir
Event sort order: Ir
Thresholds: 99
Include dirs:
User annotated:
Auto-annotation: on
--------------------------------------------------------------------------------
Ir
--------------------------------------------------------------------------------
251,772,397 PROGRAM TOTALS
--------------------------------------------------------------------------------
Ir file:function
--------------------------------------------------------------------------------
54,769,656 ???:__mcount_internal [/lib/tls/libc-2.3.4.so]
26,418,450 GBKNormalString.cpp:dictionary::CGBKNormalString::initNormalChars() [/home/search/eragon_yb/bin/iquery]
22,820,690 ???:mcount [/lib/tls/libc-2.3.4.so]
11,559,615 GBKNormalString.cpp:dictionary::CGBKNormalString::initCharKinds() [/home/search/eragon_yb/bin/iquery]
更多说明参考:
http://www-128.ibm.com/developerworks/cn/linux/l-pow-debug/
2.5. cache测试
参考:http://www.wangcong.org/articles/valgrind.html
[search@alitest146 /home/search/eragon_yb/bin]
$ valgrind --tool=cachegrind ./iquery -f ../conf/se.conf_forum -s "forum_thread?q=mp3"
==8742==
==8742== I refs: 267,968,791
==8742== I1 misses: 98,845
==8742== L2i misses: 13,382
==8742== I1 miss rate: 0.03%
==8742== L2i miss rate: 0.00%
==8742==
==8742== D refs: 182,288,669 (120,222,370 rd + 62,066,299 wr)
==8742== D1 misses: 962,816 ( 537,889 rd + 424,927 wr)
==8742== L2d misses: 707,813 ( 340,925 rd + 366,888 wr)
==8742== D1 miss rate: 0.5% ( 0.4% + 0.6% )
==8742== L2d miss rate: 0.3% ( 0.2% + 0.5% )
==8742==
==8742== L2 refs: 1,061,661 ( 636,734 rd + 424,927 wr)
==8742== L2 misses: 721,195 ( 354,307 rd + 366,888 wr)
==8742== L2 miss rate: 0.1% ( 0.0% + 0.5% )
上面的是指令缓存,I1和L2i缓存,的访问信息,包括总的访问次数,丢失次数,丢失率。
中间的是数据缓存,D1和L2d缓存,的访问的相关信息,下面的L2缓存单独的信息。Cachegrind也生成一个文件,名为 cachegrind.out.pid,可以通过cg_annotate来读取。输出是一个更详细的列表。Massif的使用和cachegrind类 似,不过它也会生成一个名为massif.pid.ps的PostScript文件,里面只有一幅描述堆栈使用状况的彩图。
[search@alitest146 /home/search/Isearchv3_Script_yb/tools]
$ ll cachegrind.out*
-rw------- 1 search search 7283 Jul 11 11:21 cachegrind.out. 8633
$ cg_annotate --8633 --auto=yes ~/isearch_yb/src/test/core/TestQuery.cpp
--------------------------------------------------------------------------------
I1 cache: 16384 B, 32 B, 8-way associative
D1 cache: 16384 B, 64 B, 8-way associative
L2 cache: 2097152 B, 64 B, 8-way associative
Command: ./iquery -f ../conf/se.conf_forum -s forum_thread?q=mp3
Data file: cachegrind.out.8633
Events recorded: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
Events shown: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
Event sort order: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
Thresholds: 99 0 0 0 0 0 0 0 0
Include dirs:
User annotated: /home/search/isearch_yb/src/test/core/TestQuery.cpp
Auto-annotation: on
--------------------------------------------------------------------------------
Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
--------------------------------------------------------------------------------
267,968,791 98,845 13,395 120,222,370 537,889 340,938 62,066,299 424,927 366,883 PROGRAM TOTALS
--------------------------------------------------------------------------------
Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw file:function
--------------------------------------------------------------------------------
56,779,152 28 6 14,194,788 82 3 14,194,788 34 13 ???:__mcount_internal
26,418,450 108 54 12,868,530 22,710 3,028 1,943,010 79,943 30,480 GBKNormalString.cpp:dictionary::CGBKNormalString::initNormalChars()
……
-- User-annotated source: get_XMLDOC.c
--------------------------------------------------------------------------------
Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
. . . . . . . . . #include "stdio.h"
. . . . . . . . . #define LINE_MAX_LEN 10240
. . . . . . . . . //get part of xml
. . . . . . . . . main(int argc,char *argv[])
10 1 1 0 0 0 1 0 0 {
. . . . . . . . . FILE *fp;
1 0 0 0 0 0 1 0 0 FILE *fpDst =NULL;
. . . . . . . . .
8 1 0 0 0 0 4 1 1 char content[LINE_MAX_LEN+1]={0};
. . . . . . . . . int inumOfdocs;
1 0 0 0 0 0 1 0 0 int currentdocs=0;
1 1 1 0 0 0 1 0 0 int isDocBegin = 0;
1 0 0 0 0 0 1 0 0 int isDocEnd = 0;
. . . . . . . . .
2 0 0 1 0 0 0 0 0 if (argc < 4)
. . . . . . . . . {
. . . . . . . . . printf("usage: get_XMLDOC srcxml dstxml numOfdocs\n");
. . . . . . . . . exit(1);
. . . . . . . . . }
. . . . . . . . .
7 2 1 2 0 0 3 0 0 inumOfdocs = atoi(argv[3]);
2 0 0 1 0 0 0 0 0 if (inumOfdocs <=0 )
相关推荐
Valgrind是一款开源的动态分析工具,主要用于检测C和C++程序中的内存错误,包括内存泄漏、未初始化的内存读取、无效指针访问等。它通过构建一个虚拟机,使得程序在该虚拟机上运行,从而能够对内存操作进行精细的监控...
对于 Linux 平台下的内存泄露检测,可以使用 Valgrind 工具。Valgrind 提供了类似的功能,支持多种不同的内存错误检查工具,如 Memcheck。Valgrind 的安装和使用相对简单,可以通过官网下载最新的版本并按照文档中的...
LeakTracer是一款专为C++程序设计的内存泄漏检测工具,主要应用于Linux、Solaris和HP-UX等操作系统环境。内存泄漏是编程过程中常见的问题,尤其是对于动态内存管理的语言如C++,它可能导致程序运行效率下降,甚至...
2. 动态内存检测:在程序运行时跟踪内存分配和释放情况,检查是否有未匹配的分配和释放。这类工具能够提供运行时的内存使用情况,帮助定位泄漏点。 3. 内存泄漏检测库:使用专门的库来替换标准的内存分配函数,通过...
对于跨平台的内存泄露检测,我们需要确保使用的工具或方法在Windows和Linux上都能正常工作。例如,Valgrind不仅支持Linux,也有Windows版本的Memcheck工具。此外,可以编写自定义的内存管理函数,如替换`new`和`...
`Valgrind for NDK` 是将 `valgrind` 工具移植到Android环境,特别是针对NDK编译的原生代码进行内存检测的一种实现。由于Android设备大多基于ARM架构,所以`valgrind for NDK` 特别强调支持ARM架构。 使用 `...
与Valgrind相比,LSan在运行速度上更快,但可能无法检测到所有类型的内存泄漏。同样,启用LSan需要在编译时添加`-fsanitize=leak`选项。 4. **Helgrind** Helgrind是Valgrind的另一个子工具,用于检测多线程程序...
Valgrind是一款强大的内存调试、性能分析和内存泄漏检测工具,尤其在Linux环境下,它被广泛用于C++程序的开发和优化。Valgrind通过构建一个虚拟机来运行你的程序,从而可以在程序运行时进行细致的监控,帮助开发者...
Valgrind适用于Linux环境,可以检测多种内存错误,包括内存泄漏。LeakSanitizer是GCC和Clang编译器内置的一种内存泄漏检测工具,它可以自动在编译时插入检查代码,无需额外的源代码修改。 总的来说,防止C/C++程序...
Linux C语言程序内存泄漏检测工具-Valgrind Valgrind 是一款 Linux 下的免费内存调试工具包,包含多个工具,如 Memcheck、Cachegrind、Helgrind、Callgrind、Massif。它可以对编译后的二进制程序进行内存使用监测...
总之,C++内存泄漏检测需要综合运用多种策略和技术,包括但不限于智能指针、自定义内存管理、使用内存检测工具等。开发者应该养成良好的编程习惯,始终关注内存的分配和释放,以编写出高效且可靠的代码。通过学习和...
18. Compuware DevPartner Java Edition:是一个 Java 内存检测工具,用于检测 Java 代码中的内存泄漏问题。 Compuware DevPartner Java Edition 工具可以检测内存泄漏问题,并提供了详细的错误信息,以便开发者...
在Linux平台上,内存泄漏是C++程序开发中常见的问题,可能导致系统资源耗尽,影响程序稳定性和性能。本文将深入探讨如何在Linux环境下检测和分析内存泄漏,主要针对C++程序。 首先,我们需要理解内存泄漏的基本概念...
通过Valgrind的Memcheck工具,我们可以检测出上述程序中存在的内存泄漏问题。编译时添加`-g`选项可以让Valgrind提供更多的调试信息: ```bash gcc -g test.c -o test ``` 运行Valgrind命令: ```bash valgrind --...
然而,需要注意的是,由于VLD依赖于Windows调试API,因此它主要适用于Windows平台的C++应用,对于跨平台的项目可能需要寻找其他内存泄漏检测工具,如Valgrind(主要用于Linux环境)。 总的来说,Visual Leak ...
Valgrind是一款强大的开源内存调试、内存泄漏检测和性能分析工具,主要针对C和C++程序。它通过创建一个虚拟的运行时环境,对程序进行逐指令模拟,从而能够检查程序在运行过程中出现的各种内存问题。Valgrind的内存...
Valgrind是一款强大的动态分析工具,专用于帮助开发者检测和解决Linux环境下的内存泄漏问题。下面将详细介绍Valgrind的工作原理、使用方法以及如何利用它来定位和修复内存泄漏。 Valgrind是一个开源的模拟执行引擎...