memory leaks C++ using umdh.exe windows

do you have a app or server process that has to run continuously..and are you running out of memory . then there is memory leak.

track the private bytes of process and see whether the private bytes are increasing through out the time. Private bytes are amount of physical RAM being used by the process. a 32 bit process cant go beyond 2 GB of RAM on windows . it can reach to 3 GB if /3GB flag is specified in C:\boot.ini .

so lets use USER MODE DUMP HEAP (UMDH.exe)  to debug memory leaks .

Steps to use umdh

1)  Download debugging tools for windows from here and install them
     http://www.microsoft.com/whdc/devtools/debugging/installx86.mspx#b

set environment variable _NT_SYMBOL_PATH.
set _NT_SYMBOL_PATH= SRV*c:\localsymbols*http://msdl.microsoft.com/download/symbols;.


for example if my c++ program is there in C:\binutils\ then give 
SRV*f:\localsymbols*http://msdl.microsoft.com/download/symbols;C:\binutils which has the .pdb files for your program. 


2) launch gflags.exe from C:\Program Files\Debugging Tools for Windows (x86)

   in the image tab, give the application name for which you want to collect leak info. we need to collect stacktrace info for this application . press tab and tick the mark for collecting stack traces.

here is the sample screenshot of gflags.exe for program.exe which is c++ app.


click apply and restart  your program. you need to restart  your program to reflect these changes.

in this example we need to restart program.exe for which i need to collect stack traces.

we can also enable user mode stack tracing for process using gflags.exe -i program.exe +ust .

+ust means enabling stack tracing

-ust means disabling stack tracing .

-i is for image name. in this case my C++ app is program.exe

step 3:

in 3rd step you need to collect heap dump using umdh.exe .
umdh.exe -g -pn:program.exe -f :dump1.txt gives the heap dump of process program.exe .

-g is option to find unreferenced memory blocks in process. its like we allocate memory using pointers again and again and forgot to free it. then the memory is not referenced any more .

-pn is option to attach umdh.exe by process name. here my process name would be program.exe

-f is for output file to which we want to write heap dumps.

step 4:
do your operations on program.exe or let your program.exe run for more time and take other heap dump.
umdh.exe -g -pn:program.exe -f :dump2.txt . so we have 2 dumps now. 


step 5:


take the diff of two dumps. this gives the increase in heap figures. umdh can also give diff between 
two dumps. 


umdh.exe -d dump1.txt dump2.txt > summary.txt . 


the above command gives diff between two dumps. the summary.txt will have this info :
memory increase in bytes and corresponding call stack which a programmer needs.


The sample summary.txt will have the following format. I attached the snapshot here


try umdh.exe for finding leaks .

many developers forgot to restart their programs after setting gflags.exe , in such cases we dont get call stacks, this is point to remember.