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
100 00AC59A8 1024 102400
num dll function offset filename line addr
0 badcgi.exe 0x1989 0x1001A6A
1 badcgi.exe 0x15c4 0x10015C8
2 badcgi.exe 0x1dbf 0x1001F1F
3 KERNEL32.dll CreateProcessW 0x3d
This shows that the stack above did 100 allocations of 1024 B, a total of 102400 B and that "badCgi.exe" 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" if it is possible in this CGI case. But usually the CGI executable will get unloaded once the request is served.
This data is enough to conclude that the culprit here is "badCgi.exel"
2- LeakDiag Logs with symbols
num dll function offset filename line addr
0 badcgi.exe doMemLeak1 0xD5 H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\cgiDBG\badcgi.cpp 494 0x1001A6A
1 badcgi.exe main 0x238 H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\cgiDBG\badcgi.cpp 162 0x10015C8
2 badcgi.exe mainCRTStartup 0xFF crtexe.c 338 0x1001F1F
3 KERNEL32.dll CreateProcessW 0x3d 0x77E9CA90
numallocs STACKID size totalsize
100 00AC59A8 1024 102400
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
100 00AC5708 1024 102400
The correspoding frames would look like:
num dll function offset filename line addr
0 badcgi.exe 0x1989 0x1001A43
1 badcgi.exe 0x15c4 0x10015C8
2 badcgi.exe 0x1dbf 0x1001F1F
3 KERNEL32.dll CreateProcessW 0x15A6 0x77E9CA90
This shows that the stack above did 100 allocations of 1024 each, a total of 102400 B allocated. This is the highest total allocations size in this log which leaves no
doubts that the culprit is "badCgi.exe".
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 badcgi.exe doMemLeak1 0xAE H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\cgiDBG\badcgi.cpp 494 0x1001A43
1 badcgi.exe main 0x238 H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\cgiDBG\badcgi.cpp 162 0x10015C8
2 badcgi.exe mainCRTStartup 0xFF crtexe.c 338 0x1001F1F
3 KERNEL32.dll CreateProcessW 0x15A6 0x77E9CA90
This shows that the function "doMemLeak1" in the program "badCgi.exe" is responsible for these allocations. This is enough
to tell the customer to review the code of this function for resolution.
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
100 00AC61C8 4096 409600
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 badcgi.exe 0x1989 0x1001A26
3 badcgi.exe 0x15c4 0x10015C8
4 badcgi.exe 0x1dbf 0x1001F1F
5 KERNEL32.dll CreateProcessW 0x15A6 0x77E9CA90
This shows that the stack above did 100 allocations of 1024 each (Virtual 1024 * 4 * 100) , a total of 4,096,000 B allocated. This is the highest total allocations size in this log which leaves no doubts that the culprit is "badCgi.exe".
2- LeakDiag Logs with symbols
numallocs STACKID size totalsize
100 00AC61C8 4096 409600
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 badcgi.exe doMemLeak1 0x91 H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\cgiDBG\badcgi.cpp 482 0x1001A26
3 badcgi.exe main 0x238 H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\cgiDBG\badcgi.cpp 162 0x10015C8
4 badcgi.exe mainCRTStartup 0xFF crtexe.c 338 0x1001F1F
5 KERNEL32.dll CreateProcessW 0x15A6 0x77E9CA90
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 function is "doMemLeak1()".
Here is how the function "doMemLeak" is implemented.
DWORD doMemLeak1(CHAR szQS[DEFAULT_LEN], int Variation)
{
char *szVar = new char[10];
char *szVal = new char[10];
getQSVars (lpEcb, szVar, szVal);
int iType;
iType = atoi( szVal );
if (1 == Variation)
{
const sizeLeaked = 100;
void* lp[sizeLeaked];
int i;
switch ( iType )
{
case 1:
for (i = 0; i < sizeLeaked; i++)
lp[i] = HeapAlloc( GetProcessHeap(), 0, 1024 ); //tracked by "WIndows Heap Allocator"
break;
case 2:
for (i = 0; i < sizeLeaked; i++)< /FONT >
lp[i] = malloc( 1024 ); //Tracked by "C Runtime Allocator"
break;
case 3:
for (i = 0; i < sizeLeaked; i++)< /FONT >
lp[i] = VirtualAlloc( NULL, 1024, MEM_COMMIT, PAGE_READWRITE ); //Tracked by "Virtual Memory Allocator"
break;
default:
for (i = 0; i < sizeLeaked; i++)
lp[i] = HeapAlloc( GetProcessHeap(), 0, 1024 ); break;
}
}
return 1;
}