프로그램의 메모리 누수(Memory Leak) 체크
이번에 소개할 툴(?) 역시 메모리 누수를 체크할 수 있는 유용한 프로그램인데, 다른 유용한 툴들이 있지만 dmalloc 또한 유용한 툴이기에 소개하고자 한다.
1. dmalloc 설치
http://dmalloc.com 사이트에서 패키지를 다운로드 한후 적당한 디렉토리에 압축을 해제한다.
설치 과정은 매우 간단해서 ./configure 를 실행한후 make ; make install 만 해주면 설치가 완료된다. 이때 설치는 root 계정으로 수행해야 한다.
2. 환경설정
# dmalloc -l logfile -I 100 high > dmalloc_setup
# cat dmalloc_setup
DMALLOC_OPTIONS=debug=0x4f47d03,inter=100,log=logfile
export DMALLOC_OPTIONS
# source dmalloc_setup
# dmalloc
Debug Malloc Utility: http://dmalloc.com/
For a list of the command-line options enter: dmalloc --usage
Debug-Flags 0x4f47d03 (83131651) (high)
Address not-set
Interval 100
Lock-On not-set
Logpath './logfile'
Start-File not-set
# cat dmalloc_setup
DMALLOC_OPTIONS=debug=0x4f47d03,inter=100,log=logfile
export DMALLOC_OPTIONS
# source dmalloc_setup
# dmalloc
Debug Malloc Utility: http://dmalloc.com/
For a list of the command-line options enter: dmalloc --usage
Debug-Flags 0x4f47d03 (83131651) (high)
Address not-set
Interval 100
Lock-On not-set
Logpath './logfile'
Start-File not-set
3. dmalloc 테스트
#include <stdio.h> #ifdef DMALLOC #include <dmalloc.h> #endif int main(void) { char *p = (char *) malloc(100); return 0; }
# gcc -g -DDMALLOC sample01.c -ldmalloc -o sample01
# cat logfile
1233132319: 1: Dmalloc version '4.8.1' from 'http://dmalloc.com/'
1233132319: 1: flags = 0x4f47503, logfile './logfile'
1233132319: 1: interval = 100, addr = 0, seen # = 0
1233132319: 1: starting time = 1233132319
1233132319: 1: free bucket count/bits: 31/7
1233132319: 1: basic-block 4096 bytes, alignment 8 bytes, heap grows up
1233132319: 1: heap: 0x804a000 to 0x804d000, size 12288 bytes (3 blocks)
1233132319: 1: heap checked 0
1233132319: 1: alloc calls: malloc 1, calloc 0, realloc 0, free 0
1233132319: 1: alloc calls: recalloc 0, memalign 0, valloc 0
1233132319: 1: total memory allocated: 100 bytes (1 pnts)
1233132319: 1: max in use at one time: 100 bytes (1 pnts)
1233132319: 1: max alloced with 1 call: 100 bytes
1233132319: 1: max alloc rounding loss: 28 bytes (21%)
1233132319: 1: max memory space wasted: 3940 bytes (96%)
1233132319: 1: final user memory space: basic 0, divided 1, 4068 bytes
1233132319: 1: final admin overhead: basic 1, divided 1, 8192 bytes (66%)
1233132319: 1: final external space: 0 bytes (0 blocks)
1233132319: 1: top 10 allocations:
1233132319: 1: total-size count in-use-size count source
1233132319: 1: 100 1 100 1 sample01.c:8
1233132319: 1: 100 1 100 1 Total of 1
1233132319: 1: dumping not-freed pointers changed since 0:
1233132319: 1: not freed: '0x804c008|s1' (100 bytes) from 'sample01.c:8'
1233132319: 1: total-size count source
1233132319: 1: 100 1 sample01.c:8
1233132319: 1: 100 1 Total of 1
1233132319: 1: known memory: 1 pointer, 100 bytes
1233132319: 1: ending time = 1233132319, elapsed since start = 0:00:00
# cat logfile
1233132319: 1: Dmalloc version '4.8.1' from 'http://dmalloc.com/'
1233132319: 1: flags = 0x4f47503, logfile './logfile'
1233132319: 1: interval = 100, addr = 0, seen # = 0
1233132319: 1: starting time = 1233132319
1233132319: 1: free bucket count/bits: 31/7
1233132319: 1: basic-block 4096 bytes, alignment 8 bytes, heap grows up
1233132319: 1: heap: 0x804a000 to 0x804d000, size 12288 bytes (3 blocks)
1233132319: 1: heap checked 0
1233132319: 1: alloc calls: malloc 1, calloc 0, realloc 0, free 0
1233132319: 1: alloc calls: recalloc 0, memalign 0, valloc 0
1233132319: 1: total memory allocated: 100 bytes (1 pnts)
1233132319: 1: max in use at one time: 100 bytes (1 pnts)
1233132319: 1: max alloced with 1 call: 100 bytes
1233132319: 1: max alloc rounding loss: 28 bytes (21%)
1233132319: 1: max memory space wasted: 3940 bytes (96%)
1233132319: 1: final user memory space: basic 0, divided 1, 4068 bytes
1233132319: 1: final admin overhead: basic 1, divided 1, 8192 bytes (66%)
1233132319: 1: final external space: 0 bytes (0 blocks)
1233132319: 1: top 10 allocations:
1233132319: 1: total-size count in-use-size count source
1233132319: 1: 100 1 100 1 sample01.c:8
1233132319: 1: 100 1 100 1 Total of 1
1233132319: 1: dumping not-freed pointers changed since 0:
1233132319: 1: not freed: '0x804c008|s1' (100 bytes) from 'sample01.c:8'
1233132319: 1: total-size count source
1233132319: 1: 100 1 sample01.c:8
1233132319: 1: 100 1 Total of 1
1233132319: 1: known memory: 1 pointer, 100 bytes
1233132319: 1: ending time = 1233132319, elapsed since start = 0:00:00
※ Dmalloc 제약사항
* Dmalloc 은 오직 힙 관련 메모리 문제들만 감지 할 수 있으며 스택 혹은 정적 메모리에서의 문제점은 감지해낼 수 없음.
* 메모리가 malloc() 에 의해 할당되었을때만 버그를 감지해낼 수 있으며 sbrk() 혹은 mmap() 에 의해 할당됐을 경우 사용이 불가능함.
* 아래와 같은 문제점들은 감지해 낼 수 없고 이것은 dbx DTC, Purify 그리고 Valgrind 같은 다른 좀더 복잡한 툴들(malloc, realloc, free 의 교체 보다 훨씬 더 많은 기능을 제공하는) 에 의해서 가능 함:
o 스택 메모리 체크
o 할당되지 않은 메모리 읽기 혹은 쓰기
o 할당됐지만 초기화 되지 않은 메모리 읽기
o 읽기전용 메모리에 쓰기