内存泄漏检测工具Valgrind学习

一句话!你的工作越接近系统,你就需要面对越多的内存问题。我觉得很帅!我喜欢挑战有难度的东西,我希望能了解最底层的系统问题,那样特别的帅!我越来越喜欢我的工作了!

Valgrind 是一款 Linux下(支持 x86、x86_64和ppc32)程序的内存调试工具,它可以对编译后的二进制程序进行内存使用监测(C语言中的malloc和free,以及C++中的new和delete),找出内存泄漏问题。

Valgrind 中包含的 Memcheck 工具可以检查以下的程序错误:

1)使用未初始化的内存 (Use of uninitialised memory)
2)使用已经释放了的内存 (Reading/writing memory after it has been free’d)
3)使用超过malloc分配的内存空间(Reading/writing off the end of malloc’d blocks)
4)对堆栈的非法访问 (Reading/writing inappropriate areas on the stack)
5)申请的空间是否有释放 (Memory leaks – where pointers to malloc’d blocks are lost forever)
6)malloc/free/new/delete申请和释放内存的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete [])
7)src和dst的重叠(Overlapping src and dst pointers in memcpy() and related functions)
8)重复free

1、编译安装 Valgrind:

wget http://valgrind.org/downloads/valgrind-3.4.1.tar.bz2
tar xvf valgrind-3.4.1.tar.bz2
cd valgrind-3.4.1/
./configure --prefix=/usr/local/webserver/valgrind
make
make instal
</pre>             <span style="font-size:18px;">2、<span style="font-family:Tahoma,Arial; background-color:rgb(234,239,240)">使用示例:对“ls”程序进程检查,返回结果中的“definitely lost: 0 bytes in 0 blocks.”表示没有内存泄漏:</span></span><p></p><p style="margin:0 0 0 40px; border:none; padding:0px"><span style="font-size:18px"><span style="font-family:Tahoma,Arial; background-color:rgb(234,239,240)"></span></span></p><pre name="code" class="html">[yaning1@inf-dev-porsche trunk]$ valgrind --tool=memcheck --leak-check=full ls
==24775== Memcheck, a memory error detector
==24775== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==24775== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==24775== Command: ls
==24775== 
aclocal.m4	db.sql	     missing	     wls.h	  wls_server.c
autom4te.cache	depcomp      package2.xml    wls.ini	  wls_server.log
config.h	ini.o	     package.xml     wls_log.c	  wls_server.o
config.h.in	install-sh   shm.o	     wls_log.h	  yac_allocator.o
config.log	-l	     stamp-h1	     wls_log.o	  yac_storage.o
config.status	lib	     storage	     wls_mysql.c
configure	Makefile     tags	     wls_mysql.h
configure.in	Makefile.am  test.php	     wls_mysql.o
cscope.out	Makefile.in  wls_client.php  wls_server
==24775== 
==24775== HEAP SUMMARY:
==24775==     in use at exit: 23,617 bytes in 46 blocks
==24775==   total heap usage: 124 allocs, 78 frees, 68,362 bytes allocated
==24775== 
==24775== LEAK SUMMARY:
==24775==    definitely lost: 0 bytes in 0 blocks
==24775==    indirectly lost: 0 bytes in 0 blocks
==24775==      possibly lost: 0 bytes in 0 blocks
==24775==    still reachable: 23,617 bytes in 46 blocks
==24775==         suppressed: 0 bytes in 0 blocks
==24775== Reachable blocks (those to which a pointer was found) are not shown.
==24775== To see them, rerun with: --leak-check=full --show-reachable=yes
==24775== 
==24775== For counts of detected and suppressed errors, rerun with: -v
==24775== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)

                3、检测我刚刚编写的wls添加及删除的接口:
[yaning1@inf-dev-porsche trunk]$ valgrind --tool=memcheck --leak-check=full ./wls_server -S127.0.0.1:9224 
==20442== Memcheck, a memory error detector
==20442== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==20442== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==20442== Command: ./wls_server -S127.0.0.1:9224
==20442== 
- [Wed May 13 11:33:30 2015] DEBUG: Start listening at 127.0.0.1:9224
- [Wed May 13 11:33:30 2015] DEBUG: Attempt to start 5 wokers
==20442== Invalid free() / delete / delete[] / realloc()
==20442==    at 0x4A07384: free (vg_replace_malloc.c:427)
==20442==    by 0x34DC10ADAA: free_mem (in /lib64/libc-2.5.so)
==20442==    by 0x34DC10A9A1: __libc_freeres (in /lib64/libc-2.5.so)
==20442==    by 0x48035DE: _vgnU_freeres (vg_preloaded.c:61)
==20442==    by 0x34DC0334E4: exit (in /lib64/libc-2.5.so)
==20442==    by 0x4C10967: yar_server_run (yar_server.c:145)
==20442==    by 0x4029DA: main (wls_server.c:661)
==20442==  Address 0x4e16b50 is not stack'd, malloc'd or (recently) free'd
==20442== 
==20442== 
==20442== HEAP SUMMARY:
==20442==     in use at exit: 220 bytes in 8 blocks
==20442==   total heap usage: 18 allocs, 12 frees, 3,271 bytes allocated
==20442== 
==20442== LEAK SUMMARY:
==20442==    definitely lost: 0 bytes in 0 blocks
==20442==    indirectly lost: 0 bytes in 0 blocks
==20442==      possibly lost: 0 bytes in 0 blocks
==20442==    still reachable: 220 bytes in 8 blocks
==20442==         suppressed: 0 bytes in 0 blocks
==20442== Reachable blocks (those to which a pointer was found) are not shown.
==20442== To see them, rerun with: --leak-check=full --show-reachable=yes
==20442== 
==20442== For counts of detected and suppressed errors, rerun with: -v
==20442== ERROR SUMMARY: 2 errors from 1 contexts (suppressed: 4 from 4)
        没有内存泄漏,很幸运,但是存在这样一句
<pre name="code" class="html">==20442== ERROR SUMMARY: 2 errors from 1 contexts (suppressed: 4 from 4)

 


        常见的错误ERROR SUMMARY:存在及翻译:

         Use of uninitialised value of size:使用了未初始化的变量值

     at 0x4C270BD: free (vg_replace_malloc.c:366):在内存释放后又进行了读和写的工作

         Invalid write of size N:表示数组越界写了N字节

         N bytes in M blocks:表示因程序退出而发生内存泄露N字节

         1 bytes in 1 blocks are definitely lost in loss record 1 of 1:发生内存泄漏,

                                                                    分配了内存没有释放

     Mismatched free() / delete delete []:不匹配的使用malloc/free、new/delete

     Invalid free() / delete delete[]: 两次释放了指针指向的内存.

    

         网站推荐:

         1)http://www.oschina.net/translate/valgrind-memcheck

         2)http://www.findfunaax.com/notes/file/179

         3)http://www.cnblogs.com/lanxuezaipiao/p/3604533.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值