dmalloc用法快速入門

來源:互聯網
上載者:User

原文連結

常用記憶體泄露檢測手段有

  • 1 mtrace
  • 2 memwatch
  • 3 mpatrol
  • 4 dmalloc
  • 5 dbgmem
  • 6 valgrind
  • 7 Electric Fence

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

 

  1. tar zxvf dmalloc-5.5.2.tgz
  2. cd dmalloc-5.5.2
  3. ./configure
  4. make
  5. 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

接下面執行:

  1. 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

  1. 1214894489: 2: Dmalloc version '5.5.2' from 'http://dmalloc.com/'
  2. 1214894489: 2: flags = 0x4e48503, logfile 'logfile'
  3. 1214894489: 2: interval = 100, addr = 0, seen # = 0, limit = 0
  4. 1214894489: 2: starting time = 1214894489
  5. 1214894489: 2: process pid = 9560
  6. 1214894489: 2: Dumping Chunk Statistics:
  7. 1214894489: 2: basic-block 4096 bytes, alignment 8 bytes
  8. 1214894489: 2: heap address range: 0xb8020000 to 0xb8029000, 36864 bytes
  9. 1214894489: 2:     user blocks: 1 blocks, 4043 bytes (10%)
  10. 1214894489: 2:    admin blocks: 8 blocks, 32768 bytes (89%)
  11. 1214894489: 2:    total blocks: 9 blocks, 36864 bytes
  12. 1214894489: 2: heap checked 1
  13. 1214894489: 2: alloc calls: malloc 2, calloc 0, realloc 0, free 0
  14. 1214894489: 2: alloc calls: recalloc 0, memalign 0, valloc 0
  15. 1214894489: 2: alloc calls: new 0, delete 0
  16. 1214894489: 2:   current memory in use: 11 bytes (2 pnts)
  17. 1214894489: 2:  total memory allocated: 11 bytes (2 pnts)
  18. 1214894489: 2:  max in use at one time: 11 bytes (2 pnts)
  19. 1214894489: 2: max alloced with 1 call: 6 bytes
  20. 1214894489: 2: max unused memory space: 53 bytes (82%)
  21. 1214894489: 2: top 10 allocations:
  22. 1214894489: 2:  total-size  count in-use-size  count  source
  23. 1214894489: 2:           6      1           6      1  dm_test.c:71
  24. 1214894489: 2:           5      1           5      1  dm_test.c:69
  25. 1214894489: 2:          11      2          11      2  Total of 2
  26. 1214894489: 2: Dumping Not-Freed Pointers Changed Since Start:
  27. 1214894489: 2:  not freed: '0xb8028fc8|s1' (6 bytes) from 'dm_test.c:71'
  28. 1214894489: 2:  not freed: '0xb8028fe8|s1' (5 bytes) from 'dm_test.c:69'
  29. 1214894489: 2:  total-size  count  source
  30. 1214894489: 2:           6      1  dm_test.c:71
  31. 1214894489: 2:           5      1  dm_test.c:69
  32. 1214894489: 2:          11      2  Total of 2
  33. 1214894489: 2: ending time = 1214894489, elapsed since start = 0:00:00

 

那麼,哪個地方的記憶體leak就一目瞭然了。

//====== dm_test.c 原始碼 =============

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #ifdef DMALLOC
  6. #include <dmalloc.h>
  7. #endif
  8.  
  9. int main(int argc, char **argv)
  10. {
  11.    
  12.     char *str;
  13.  
  14.     str = malloc(5);
  15.  
  16.     str = malloc(6);
  17.    
  18.     return 0;
  19. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.