This package includes memtrace.h, heaptrace.h, memtrace_com.h and memtrace.lib. To use this package in win32 system to trace HeapAlloc, including <windows.h> before "memtrace.h". To use this package without tracing HeapAlloc, you can use it to trace whatever apps conforms to ANSI C. This package will make your life easier to locate the memory leaks fast. It's also a useful tool to analyze your app's memory usage and find out which code consumes much memory.
Trace HeapAlloc, malloc, new allocated memory.
Trace Total allocated memory, total freed memory, peek memory usage.
Trace overal memory usage distributions into file and line. Trace peek memory distributions.
Trace stack size.
Output memory trace log file.
Include header file
Add Line: #include "memtrace.h" into your header file.
Before this line, define macros to specify which memory you want to trace.
Sample code:
#define TRACE_MALLOC 0
#define TRACE_NEW 0
#define TRACE_HEAP 1
#define TRACE_STACK 1
#include "memtrace.h"
MACRO | VALUE | USAGE |
TRACE_MALLOC | 1(on) / 0(off) | once defined it as 1, it will trace all the malloc that appears in source files, which included the memtrace.h. It will give you logs which file/line calls malloc and how much is allocated and freed in this location. |
TRACE_NEW | 1(on) / 0(off) | once defined it as 1, it will trace the total new allocated memory. To enable tracing into details as which file/line totally allocated/freed how much memory, It is REQUIRED to put MARK_THIS_POSITION right before each new calls. |
TRACE_HEAP | 1(on) / 0(off) | as trace_malloc, it will trace all the HeapAlloc into details. |
TRACE_STACK | 1(on) / 0(off) | This will enable tracing the stack depth. To trace stack
usage of specific function and its called. Put
STACK_MEASURE_START(stack_no) to the first statement of the function. Put
STACK_MEASURE_STOP(stack_depth, stack_no) to the last statement of the
function. stack_depth contains the call stack depth of this function in
bytes. To trace stack usage for the entire application, because each thread has its own call stack, you need to trace all of them. STACK_MEASURE_START(stack_no) gives you capability to trace several stacks at the same time. Specify the stack_no from 0 to the total of your stacks, put STACK_MEASURE_START() to the entry point of each thread and STACK_MEASURE_STOP() to the exit point, and sum all of the stack_depth, you will get the stack depth of the entire application. STACK_MEASURE_START() and STACK_MEASURE_STOP MUST put into same function. |
If anyone of TRACE_MALLOC, TRACE_NEW and TRACE_HEAP is on, you
will need put HEAP_MEASURE_START() to the start point that you want to trace
the memory usage, Put WRITE_MEM_LOG to output memory usage to log files ( "\stat.log"
). If HEAP_MEASURE_START() is called twice, the last call will eliminate the
first tracing results.
Link lib
Add memtrace.lib to your linked lib.
Tracing HeapAlloc/malloc/new for single thread console app:
#define TRACE_MALLOC 1
#define TRACE_NEW
1
#define TRACE_HEAP
1
#include "memtrace.h"
int main()
{
HEAP_MEASURE_START()
your code ...
WRITE_MEM_LOG
}
Tracing Stack for single thread console app:
#define TRACE_STACK 1
#include "memtrace.h"
int main()
{
STACK_MEASURE_START(0)
your code ...
STACK_MEASURE_STOP(stack_depth, 0)
printf("Total Stack Depth: %d", stack_depth);
}
*********************
Peek Ram Used: 216343
Peek Ram Distributions:
272 bytes(0.001257 percent) in D:\build\multimedia\src\v8dme\Network\client\dstruct\vcelpool.cpp
line 724.
1284 bytes(0.005935 percent) in ..\common\baseclient.h line 64.
800 bytes(0.003698 percent) in ..\common\basesession.h line 72.
592 bytes(0.002736 percent) in ..\wince\asyncresult.h line 179.
20 bytes(0.000092 percent) in ..\include\wmsstring.h line 1055.
520 bytes(0.002404 percent) in D:\build\multimedia\src\v8dme\Network\client\wince\workqueue.h
line 294.
168 bytes(0.000777 percent) in D:\build\multimedia\src\v8dme\Network\client\wince\asyncresult.h
line 179.
1380 bytes(0.006379 percent) in ..\common\basechannel.h line 65.
171691 bytes(0.793606 percent) in D:\build\multimedia\src\v8dme\Network\client\common\netbuffer.h
line 59.
32 bytes(0.000148 percent) in D:\build\multimedia\src\v8dme\Network\client\wince\socketimp.cpp
line 779.
1334 bytes(0.006166 percent) in ..\common\netbuffer.h line 59.
6608 bytes(0.030544 percent) in D:\build\multimedia\src\v8dme\Network\client\common\netbuffer.h
line 616.
80 bytes(0.000370 percent) in D:\build\multimedia\src\v8dme\Network\client\wince\socketimp.cpp
line 665.
15611 bytes(0.072159 percent) in D:\build\multimedia\src\v8dme\Network\client\wince\socketimp.cpp
line 678.
60 bytes(0.000277 percent) in D:\build\multimedia\src\v8dme\Network\client\common\netbuffer.cpp
line 64.
1024 bytes(0.004733 percent) in D:\build\multimedia\src\v8dme\Network\client\common\incrementalalloc.h
line 120.
328 bytes(0.001516 percent) in ..\common\netbuffer.h line 616.
6251 bytes(0.028894 percent) in ..\common\incrementalalloc.h line 120.
3160 bytes(0.014606 percent) in ..\common\ipresentationdescription.h line 133.
1824 bytes(0.008431 percent) in D:\build\multimedia\src\v8dme\Network\client\netstream\netstream.cpp
line 1506.
Memory Statistics:
Allocated Total Memory: 2936922 bytes.
Allocated Free Memory: 2900437 bytes.
HeapAlloc Memory: 2936922 bytes.
Malloc Memory: 0 bytes.
New Memory: 0 bytes.
Maximum Ram Used: 221266 bytes.
Detailed Heap Allocated.
292 bytes in d:\build\multimedia\src\v8dme\network\client\dstruct\vcelpool.cpp
722 line. Remain 292 bytes.
2444 bytes in d:\build\multimedia\src\v8dme\network\client\common\baseclient.h
64 line. Remain 2444 bytes.
1320 bytes in d:\build\multimedia\src\v8dme\network\client\common\basesession.h
72 line. Remain 1320 bytes.
64572 bytes in d:\build\multimedia\src\v8dme\network\client\wince\asyncresult.h
179 line. Remain 816 bytes.
13437 bytes in d:\build\multimedia\src\v8dme\network\client\include\wmsstring.h
1055 line. Remain 20 bytes.
520 bytes in d:\build\multimedia\src\v8dme\network\client\wince\workqueue.h 294
line. Remain 520 bytes.
1384 bytes in d:\build\multimedia\src\v8dme\network\client\common\basechannel.h
65 line. Remain 1384 bytes.
11464 bytes in d:\build\multimedia\src\v8dme\network\client\common\inetrequest.h
201 line. Remain 1344 bytes.
1069484 bytes in d:\build\multimedia\src\v8dme\network\client\common\netbuffer.h
59 line. Remain 10720 bytes.
9952 bytes in D:\build\multimedia\src\v8dme\NETWORK\CLIENT\WINCE\socketimp.cpp
779 line. Remain 96 bytes.
1365784 bytes in d:\build\multimedia\src\v8dme\network\client\common\netbuffer.h
616 line. Remain 3980 bytes.
2024 bytes in D:\build\multimedia\src\v8dme\NETWORK\CLIENT\WINCE\socketimp.cpp
1879 line. Remain 78 bytes.
6832 bytes in d:\build\multimedia\src\v8dme\network\client\wince\workqueue.cpp
206 line. Remain 0 bytes.
1904 bytes in D:\build\multimedia\src\v8dme\NETWORK\CLIENT\WINCE\socketimp.cpp
665 line. Remain 0 bytes.
351469 bytes in D:\build\multimedia\src\v8dme\NETWORK\CLIENT\WINCE\socketimp.cpp
678 line. Remain 0 bytes.
60 bytes in d:\build\multimedia\src\v8dme\network\client\common\netbuffer.cpp 64
line. Remain 60 bytes.
28332 bytes in d:\build\multimedia\src\v8dme\network\client\common\incrementalalloc.h
120 line. Remain 8299 bytes.
512 bytes in d:\build\multimedia\src\v8dme\network\client\common\inetrequest.h
83 line. Remain 0 bytes.
3160 bytes in d:\build\multimedia\src\v8dme\network\client\common\ipresentationdescription.h
133 line. Remain 3160 bytes.
1952 bytes in D:\build\multimedia\src\v8dme\Network\client\netstream\netstream.cpp
1508 line. Remain 1952 bytes.
24 bytes in d:\build\multimedia\src\v8dme\network\client\wince\asyncresult.h 356
line. Remain 0 bytes.
Detailed New Allocated.
Detailed Malloc Allocated.
last edit on 08/15/2002