感谢支持
我们一直在努力

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个内存错误,看样子,www.linuxidc.com我有的忙了,不过幸好这个工具给出了具体出错的位置,不用花几天的功夫去检测!!! 


以下是从网上摘录的


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

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




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

  2. h –help 显示帮助信息。

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

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

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

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

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

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

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

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

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

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

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

LOG信息输出




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

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

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

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

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

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

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




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

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

  3. -show-reachable=no|yes show reachable blocks in leak check? [no]

赞(0) 打赏
转载请注明出处:服务器评测 » Ubuntu下安装内存检测工具Valgrind
分享到: 更多 (0)

听说打赏我的人,都进福布斯排行榜啦!

支付宝扫一扫打赏

微信扫一扫打赏