Ubuntu下安装内存检测工具Valgrind

Ubuntu下安装Valgrind很简单,直接从ubuntu软件中心下载安装,并且还会提供一个简单的图形界面。。当然,你也可以在命令行中使用。

Valgrind实际上是一个工具集,包含多个调试工具,如常用的memcheck,当程序日渐庞大时,内存泄漏是个很头疼的问题,可以使用如下命令,来检测:

valgrind --tool=memcheck --leak-check=yes ./main

其中./main是需要检测的程序。当程序运行结束或中止时,会打印出内存信息。

这是本人运行程序过程中强制终止的信息:

=3516== HEAP SUMMARY:
==3516==     in use at exit: 691,535 bytes in 912 blocks
==3516==   total heap usage: 1,168 allocs, 256 frees, 2,524,201 bytes allocated
==3516==
==3516== 8 bytes in 1 blocks are definitely lost in loss record 180 of 587
==3516==    at 0x4026864: malloc (vg_replace_malloc.c:236)
==3516==    by 0x804D636: AnalyzeCBlock (AnalyzePage.c:1706)
==3516==    by 0x804DA51: AnalyzePage (AnalyzePage.c:1825)
==3516==    by 0x80645DC: GetTGPFromSoGouBySA (wrapper.c:1148)
==3516==    by 0x8068BB6: ExeOneDS (wrapper.c:2847)
==3516==    by 0x8068F98: ExecuteDataSource (wrapper.c:2972)
==3516==    by 0x80541BC: SearchPlan (SearchPlan.c:263)
==3516==    by 0x8054847: ThreadFunction_Regular (UserRegular.c:105)
==3516==    by 0x8052C14: wrapper_fn (ThreadPool.c:100)
==3516==    by 0x4063E98: start_thread (pthread_create.c:304)
==3516==    by 0x41475FD: clone (clone.S:130)
==3516==
==3516== 8 bytes in 1 blocks are definitely lost in loss record 181 of 587
==3516==    at 0x4026864: malloc (vg_replace_malloc.c:236)
==3516==    by 0x804D636: AnalyzeCBlock (AnalyzePage.c:1706)
==3516==    by 0x804DA51: AnalyzePage (AnalyzePage.c:1825)
==3516==    by 0x8065A70: GetAlbumFromBaiduBySinger (wrapper.c:1602)
==3516==    by 0x8068BB6: ExeOneDS (wrapper.c:2847)
==3516==    by 0x8068D99: ExecuteDataSource (wrapper.c:2916)
==3516==    by 0x80541BC: SearchPlan (SearchPlan.c:263)
==3516==    by 0x8054847: ThreadFunction_Regular (UserRegular.c:105)
==3516==    by 0x8052C14: wrapper_fn (ThreadPool.c:100)
==3516==    by 0x4063E98: start_thread (pthread_create.c:304)
==3516==    by 0x41475FD: clone (clone.S:130)
..........................

.......

==3516== LEAK SUMMARY:
==3516==    definitely lost: 768 bytes in 13 blocks
==3516==    indirectly lost: 3,728 bytes in 134 blocks
==3516==      possibly lost: 544 bytes in 4 blocks
==3516==    still reachable: 686,495 bytes in 761 blocks
==3516==         suppressed: 0 bytes in 0 blocks
==3516== Reachable blocks (those to which a pointer was found) are not shown.
==3516== To see them, rerun with: --leak-check=full --show-reachable=yes
==3516==
==3516== For counts of detected and suppressed errors, rerun with: -v
==3516== Use --track-origins=yes to see where uninitialised values come from
==3516== ERROR SUMMARY: 32 errors from 24 contexts (suppressed: 33 from 8)

在最后会给出统计信息,32个内存错误,看样子,我有的忙了,不过幸好这个工具给出了具体出错的位置,不用花几天的功夫去检测!!! 

以下是从网上摘录的

///////////////////////////////////////////////////////////////////////////////////////////

用法: valgrind [options] prog-and-args [options]: 常用选项,适用于所有Valgrind 工具

-tool=<name> 最常用的选项。运行 valgrind 中名为toolname的工具。默认memcheck。

h –help 显示帮助信息。

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

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

v –verbose 更详细的信息, 增加错误数统计。

-trace-children=no|yes 跟踪子线程? [no]

-track-fds=no|yes 跟踪打开的文件描述?[no]

-time-stamp=no|yes 增加时间戳到LOG信息? [no]

-log-fd=<number> 输出LOG到描述符文件 [2=stderr]

-log-file=<file> 将输出的信息写入到filename.PID的文件里,PID是运行程序的进行ID

-log-file-exactly=<file> 输出LOG信息到 file

-log-file-qualifier=<VAR> 取得环境变量的值来做为输出信息的文件名。 [none]

-log-socket=ipaddr:port 输出LOG到socket ,ipaddr:port

LOG信息输出

-xml=yes 将信息以xml格式输出,只有memcheck可用

-num-callers=<number> show <number> callers in stack traces [12]

-error-limit=no|yes 如果太多错误,则停止显示新错误? [yes]

-error-exitcode=<number> 如果发现错误则返回错误代码 [0=disable]

-db-attach=no|yes 当出现错误,valgrind 会自动启动调试器gdb。[no]

-db-command=<command> 启动调试器的命令行选项[gdb -nw %f %p]

适用于Memcheck工具的相关选项:

-leak-check=no|summary|full 要求对leak给出详细信息? [summary]

-leak-resolution=low|med|high how much bt merging in leak check [low]

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/23121.html