日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

内存泄露检测工具之DMalloc

發布時間:2025/3/17 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 内存泄露检测工具之DMalloc 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

dmalloc用法快速入門


dmalloc是一個簡單易用的C/C++內存leak檢查工具,以一個運行庫的方式發布。

dmalloc能夠檢查出直到程序運行結束還沒有釋放的內存,并且能夠精確指出在
哪個源文件的第幾行。

dmalloc 主頁:?http://dmalloc.com

支持的平臺:AIX, BSD/OS, DG/UX, Free/Net/OpenBSD, GNU/Hurd, HPUX, Irix, Linux, MS-DOG, NeXT, OSF, SCO, Solaris, SunOS, Ultrix, Unixware, Windoze, and even Unicos on a Cray T3E

最新版本: 5.5.2

安裝:下載?http://dmalloc.com/releases/dmalloc-5.5.2.tgz

  • tar?zxvf dmalloc-5.5.2.tgz
  • cd?dmalloc-5.5.2
  • ./configure
  • make
  • make?install
  • 設置環境變量:
    對于 Bash, ksh, and zsh (http://www.zsh.org/), 在 `.bashrc', `.profile', or `.zshrc'
    文件中加入一行 ( -b 參數表示針對Bash的輸出):

    function dmalloc { eval `command dmalloc -b $*`; }

    然后重新登陸用戶,或者執行: source ~/.bashrc 或 source ~/.profile

    接下面執行:

  • dmalloc -l logfile -i?100?low
  • 在源文件中添加下面的C代碼:

    #ifdef DMALLOC
    #include "dmalloc.h"
    #endif

    編譯使用的CFLAGS: -DDMALLOC -DDMALLOC_FUNC_CHECK

    如: gcc -DDMALLOC -DDMALLOC_FUNC_CHECK dm_test.c

    執行:
    ./a.out

    運行上面的命令會在當前目錄下生成 logfile文件,查看logfile的內容如下:

    cat logfile

  • 1214894489: 2: Dmalloc version '5.5.2' from 'http://dmalloc.com/'
  • 1214894489: 2: flags = 0x4e48503, logfile 'logfile'
  • 1214894489: 2: interval = 100, addr = 0, seen # = 0, limit = 0
  • 1214894489: 2: starting time = 1214894489
  • 1214894489: 2: process pid = 9560
  • 1214894489: 2: Dumping Chunk Statistics:
  • 1214894489: 2: basic-block 4096 bytes, alignment 8 bytes
  • 1214894489: 2: heap address range: 0xb8020000 to 0xb8029000, 36864 bytes
  • 1214894489: 2: ? ? user blocks: 1 blocks, 4043 bytes (10%)
  • 1214894489: 2: ? ?admin blocks: 8 blocks, 32768 bytes (89%)
  • 1214894489: 2: ? ?total blocks: 9 blocks, 36864 bytes
  • 1214894489: 2: heap checked 1
  • 1214894489: 2: alloc calls: malloc 2, calloc 0, realloc 0, free 0
  • 1214894489: 2: alloc calls: recalloc 0, memalign 0, valloc 0
  • 1214894489: 2: alloc calls: new 0, delete 0
  • 1214894489: 2: ? current memory in use: 11 bytes (2 pnts)
  • 1214894489: 2: ?total memory allocated: 11 bytes (2 pnts)
  • 1214894489: 2: ?max in use at one time: 11 bytes (2 pnts)
  • 1214894489: 2: max alloced with 1 call: 6 bytes
  • 1214894489: 2: max unused memory space: 53 bytes (82%)
  • 1214894489: 2: top 10 allocations:
  • 1214894489: 2: ?total-size ?count in-use-size ?count ?source
  • 1214894489: 2: ? ? ? ? ? 6 ? ? ?1 ? ? ? ? ? 6 ? ? ?1 ?dm_test.c:71
  • 1214894489: 2: ? ? ? ? ? 5 ? ? ?1 ? ? ? ? ? 5 ? ? ?1 ?dm_test.c:69
  • 1214894489: 2: ? ? ? ? ?11 ? ? ?2 ? ? ? ? ?11 ? ? ?2 ?Total of 2
  • 1214894489: 2: Dumping Not-Freed Pointers Changed Since Start:
  • 1214894489: 2: ?not freed: '0xb8028fc8|s1' (6 bytes) from 'dm_test.c:71'
  • 1214894489: 2: ?not freed: '0xb8028fe8|s1' (5 bytes) from 'dm_test.c:69'
  • 1214894489: 2: ?total-size ?count ?source
  • 1214894489: 2: ? ? ? ? ? 6 ? ? ?1 ?dm_test.c:71
  • 1214894489: 2: ? ? ? ? ? 5 ? ? ?1 ?dm_test.c:69
  • 1214894489: 2: ? ? ? ? ?11 ? ? ?2 ?Total of 2
  • 1214894489: 2: ending time = 1214894489, elapsed since start = 0:00:00
  • 那么,哪個地方的內存leak就一目了然了。

    //====== dm_test.c 源代碼 =============

  • #include <stdio.h>
  • #include <stdlib.h>
  • #include <string.h>
  • #ifdef DMALLOC
  • #include <dmalloc.h>
  • #endif
  • int?main(int?argc,?char?**argv)
  • {
  • char?*str;
  • str?=?malloc(5);
  • str?=?malloc(6);
  • return?0;
  • }
  • 參見文檔:

    http://dmalloc.com/docs/latest/dmalloc.pdf.gz

    總結

    以上是生活随笔為你收集整理的内存泄露检测工具之DMalloc的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。