1) What is memory leak?
Memory Leaks show up as an increasing Private Virtual size. At some point, the memory manager will stop the process from growing in physical size, though it can continue to
grow in its virtual size until it reaches around 1.5 GB.
2) Tracking "Windows Heap Allocator"
1- LeakDiag Logs without symbols
Using LDParser that you can download from http://leakdiag to read the logs, open one of the logs and make sure the "Stacks" table is sorted on "totalsize" column.
Select the first row in the "Stacks" table if you sorted on "totalsise" decreasing.
numallocs STACKID size totalsize
39 02346708 1024 39936
num dll function offset filename line addr
0 badext.dll HttpExtensionProc 0x54C 0x261186F
1 badext.dll HttpExtensionProc 0x1C7 0x26114EA
2 wam.dll SE_TABLE__ReleaseExtension 0xBE8 0x65D82B06
3 wam.dll SE_TABLE__ReleaseExtension 0x484 0x65D823A2
4 RPCRT4.DLL NdrServerInitialize 0x288 0x77D339C0
This shows that the stack above did 39 allocations of 1024 B, a total of 39936 B and that "badext.dll" is the one that made these allocations.
For precise reading of leakDiag logs, it is important that you compare the totalsize of allocations of the suspected stack to the increase you see in
performance monitor in "Private Bytes".
This data is enough to conclude that the culprit here is "badext.dll"
2- LeakDiag Logs with symbols
num dll function offset filename line addr
0 badext.dll doMemLeak1 0x54C 0x261186F
1 badext.dll HttpExtensionProc 0x1C7 0x26114EA
2 wam.dll ExecuteRequest 0xBE8 0x65D82B06
3 wam.dll InvokeExtension 0x484 0x65D823A2
4 wam.dll ProcessRequest 0x288 0x77D339C0
numallocs STACKID size totalsize
39 02346708 1024 39936
Back in Windbg, open the dump file generated by leakDiag to double check findings:
The addresses that LeakDiag logs show in the stack are where the functions are loaded in memory.
The "ln" debugger command shows what module!function is loaded in the specified address.
0:027> ln 0x261186F
(026117fd) badext!doMemLeak1+0x72
0:027> ln 0x26114EA
(02611323) badext!HttpExtensionProc+0x1c7
You could use these addresses as well to walk through the disassembly for indepth analysis.
3) Tracking "C Runtime Allocator"
1- LeakDiag Logs without symbols
Open a leakDiag log, make sure that the table "STACKS" is sorted, select the row that corresponds to the highest "totalsize"
numallocs STACKID size totalsize
70 02344468 1024 71680
The correspoding frames would look like:
num dll function offset filename line addr
0 badext.dll HttpExtensionProc 0x535 0x2601858
1 badext.dll HttpExtensionProc 0x1C7 0x26014EA
2 wam.dll SE_TABLE__ReleaseExtension 0xBE8 0x65D82B06
3 wam.dll SE_TABLE__ReleaseExtension 0x484 0x65D823A2
4 RPCRT4.DLL NdrServerInitialize 0x288 0x77D339C0
This shows that the stack above did 70 allocations of 1024 each, a total of 71680 B allocated. This is the highest total allocations size in this log which leaves no
doubts that the culprit is "badext.dll".
2- LeakDiag Logs with symbols
numallocs STACKID size totalsize
70 02344468 1024 71680
The correspoding frames would look like:
num dll function offset filename line addr
0 badext.dll doMemLeak1 0x5b 0x2601858
1 badext.dll HttpExtensionProc 0x1C7 0x26014EA
2 wam.dll ExecuteRequest 0x93 0x65D82B06
3 wam.dll ProcessRequest 0x240 0x65D823A2
4 RPCRT4. DLL Invoke 0x30 0x77D339C0
This shows that the function "doMemLeak1" in the module "badext.dll" is responsible for these allocations. This is enough
to tell the customer to review the code of this function for resolution.
Sometimes, you may want to provide additional data from the dump to the customer to show what data is leaked especially if you took 2 or 3 dumps (not logs)
during a leakDiag session. These dumps will allow you to identify what heap segment(s) are leaking.
So, in each dump, issue the command "!heap 0 0" and notice what heap index grew in memory committed.
0:028> !heap 0 0
Index Address Name Debugging options enabled
1: 00070000
Segment at 00070000 to 00170000 (00010000 bytes committed)
2: 00170000
Segment at 00170000 to 00180000 (00001000 bytes committed)
3: 00400000
Segment at 00400000 to 00410000 (00010000 bytes committed)
Segment at 02610000 to 02710000 (00100000 bytes committed)
Segment at 04A90000 to 04C90000 (0001b000 bytes committed)
4: 00420000
Segment at 00420000 to 00430000 (00003000 bytes committed)
.......
0:028> !heap 0 0
Index Address Name Debugging options enabled
1: 00070000
Segment at 00070000 to 00170000 (0005d000 bytes committed)
2: 00170000
Segment at 00170000 to 00180000 (00001000 bytes committed)
3: 00400000
Segment at 00400000 to 00410000 (00010000 bytes committed)
Segment at 02610000 to 02710000 (00100000 bytes committed)
Segment at 04A90000 to 04C90000 (0001b000 bytes committed)
4: 00420000
Segment at 00420000 to 00430000 (00003000 bytes committed)
.......
This shows that heap index 1 grew in memory committed by 315392 B:
0:028> ? (0005d000 - 00010000)
Evaluate expression: 315392 = 0004d000
Dumping the address of heap index 1 (00070000 to 00170000 ) may show some data that could be relevent. In our case, we did not copy
any data in the heap we allocated, so it looks like:
0:028> dc 00070000
00070000 000000c8 00000100 eeffeeff 00000002 ................
00070010 00000000 0000fe00 00100000 00002000 ............. ..
00070020 00000200 00002000 0000088c 7ffdefff ..... ..........
00070030 06080001 00000000 00000000 00000000 ................
00070040 00000000 000705a8 0000000f fffffff8 ................
00070050 00070050 00070050 00070640 00000000 P...P...@.......
00070060 00000000 00000000 00000000 00000000 ................
00070070 00000000 00000000 00000000 00000000 ................
4) Tracking "Virtual Memory Allocator"
Open a leakDiag log generated while tracking "Virtual Memory Allocator", make sure that the table "STACKS" is sorted, select the row that corresponds to the highest "totalsize"
numallocs STACKID size totalsize
256 026F4008 4096 1048576
The correspoding frames would look like:
num dll function offset filename line addr
0 KERNEL32.DLL VirtualAllocEx 0x5D 0x77E87FC7
1 KERNEL32.DLL VirtualAlloc 0x16 0x77E87F66
2 badext.dll HttpExtensionProc 0x528 0x21E184B
3 badext.dll HttpExtensionProc 0x1C7 0x21E14EA
4 wam.dll SE_TABLE__ReleaseExtension 0xBE8 0x65D82B06
This shows that the stack above did 256 allocations of 1024 each, a total of 1048576 B allocated. This is the highest total allocations size in this log which leaves no
doubts that the culprit is "badext.dll".
2- LeakDiag Logs with symbols
numallocs STACKID size totalsize
256 026F4008 4096 1048576
The correspoding frames would look like:
num dll function offset filename line addr
0 KERNEL32.DLL VirtualAllocEx 0x62 0x77E87FC7
1 KERNEL32.DLL VirtualAlloc 0x16 0x77E87F66
2 badext.dll doMemLeak1 0x4e 0x21E184B
3 badext.dll HttpExtensionProc 0x1C7 0x21E14EA
4 wam.dll ExecuteReques 0x93 0x65D82B06
With symbols, we can see that the function "doMemLeak1()" is the one calling the win32 API "virtualAlloc()" to allocate virtual memory.
Again, this is enough to confirm that the culprit is "badext.dll".
Here is how the function "doMemLeak" is implemented.
DWORD doMemLeak1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation)
{
char *szVar = new char[10];
char *szVal = new char[10];
getQSVars (lpEcb, szVar, szVal);
int iType;
iType = atoi( szVal );
if (1 == Variation)
{
void* lp = NULL;
switch ( iType )
{
case 1: lp = HeapAlloc( GetProcessHeap(), 0, 1024 ); //tracked by "WIndows Heap Allocator"
break;
case 2: lp = malloc( 1024 ); //Tracked by "C Runtime Allocator"
break;
case 3: lp = VirtualAlloc( NULL, 1024, MEM_COMMIT, PAGE_READWRITE ); //Tracked by "Virtual Memory Allocator"
break;
default: lp = HeapAlloc( GetProcessHeap(), 0, 1024 ); break;
}
}
return HSE_STATUS_SUCCESS;
}