finding memory leaks with umdh
DESCRIPTION
A brief introduction to UMDH, a tool for finding memory leaks in Windows applications.TRANSCRIPT
![Page 1: Finding Memory Leaks with UMDH](https://reader031.vdocuments.us/reader031/viewer/2022020306/555c42d9d8b42a2c068b4ea6/html5/thumbnails/1.jpg)
![Page 2: Finding Memory Leaks with UMDH](https://reader031.vdocuments.us/reader031/viewer/2022020306/555c42d9d8b42a2c068b4ea6/html5/thumbnails/2.jpg)
Stands for User Mode Dump Heap
Can be downloaded for free as part of the Windows 7 SDK (also works for Windows XP)
Helps you to find memory leaks in running processes!
![Page 3: Finding Memory Leaks with UMDH](https://reader031.vdocuments.us/reader031/viewer/2022020306/555c42d9d8b42a2c068b4ea6/html5/thumbnails/3.jpg)
1. Set the _NT_SYMBOL_PATH environment var to:
2. Append the path to your executable
srv*c:\symbols*http://msdl.microsoft.com/download/symbols;
![Page 4: Finding Memory Leaks with UMDH](https://reader031.vdocuments.us/reader031/viewer/2022020306/555c42d9d8b42a2c068b4ea6/html5/thumbnails/4.jpg)
1. Flag your process under test as being monitored:
2. Start your process under test:
3. Take an initial “snapshot” of allocated memory as Test.exe runs:
C:> gflags -i Test.exe +ust
C:> umdh -pn:Test.exe -f:C:\f1.txt
C:> Test.exe
![Page 5: Finding Memory Leaks with UMDH](https://reader031.vdocuments.us/reader031/viewer/2022020306/555c42d9d8b42a2c068b4ea6/html5/thumbnails/5.jpg)
4. Exercise your program in a way that you think may make it leak memory.
5. Take a second snapshot and save it with a different file name from the first.
6. Use UMDH to “diff” the first and last snapshots taken to get the best idea of what has leaked
C:> umdh -d f1.txt f2.txt > f12.txt
C:> umdh -pn:Test.exe -f:C:\f2.txt
![Page 6: Finding Memory Leaks with UMDH](https://reader031.vdocuments.us/reader031/viewer/2022020306/555c42d9d8b42a2c068b4ea6/html5/thumbnails/6.jpg)
The “diff” file shows a stack trace, and the
number/size of allocations that were allocated but not freed between two snapshots.
+ 483328 ( 487424 - 4096) 119 allocs BackTrace23FF
+ 118 ( 119 - 1) BackTrace23FF allocations
ntdll!RtlDebugAllocateHeap+000000E1
ntdll!RtlAllocateHeapSlowly+00000044
ntdll!RtlAllocateHeap+00000E64
kernel32!FindNextFileW+00000077
kernel32!FindNextFileA+00000028
MSVCR80D!findnext64i32+00000178
Test!readdir+00000075 (c:\myproject\util.cpp, 674)
…
![Page 7: Finding Memory Leaks with UMDH](https://reader031.vdocuments.us/reader031/viewer/2022020306/555c42d9d8b42a2c068b4ea6/html5/thumbnails/7.jpg)
+ 483328 ( 487424 - 4096) 119 allocs BackTrace23FF
+ 118 ( 119 - 1) BackTrace23FF allocations
ntdll!RtlDebugAllocateHeap+000000E1
ntdll!RtlAllocateHeapSlowly+00000044
ntdll!RtlAllocateHeap+00000E64
kernel32!FindNextFileW+00000077
kernel32!FindNextFileA+00000028
MSVCR80D!findnext64i32+00000178
Test!readdir+00000075 (c:\myproject\util.cpp, 674)
…
Bytes “leaked” between the two snapshots
Stacktrace showing where the “leaked”
memory was allocated.
![Page 8: Finding Memory Leaks with UMDH](https://reader031.vdocuments.us/reader031/viewer/2022020306/555c42d9d8b42a2c068b4ea6/html5/thumbnails/8.jpg)
At the very bottom of the file, there is an overall summary of how much memory was allocated but not freed. E.g.:
If used as part of an automated test, it is fairly easy to extract the “Total increase” number using a Perl script and graphs of memory allocations and usage over time.
Total increase == 13919896 requested + 830364 overhead = 14750260
![Page 9: Finding Memory Leaks with UMDH](https://reader031.vdocuments.us/reader031/viewer/2022020306/555c42d9d8b42a2c068b4ea6/html5/thumbnails/9.jpg)
UMDH is a useful tool for finding memory leaks in Windows applications.
Its output is a file listing outstanding memory allocations that can be read directly, or processed by another program or script.
![Page 10: Finding Memory Leaks with UMDH](https://reader031.vdocuments.us/reader031/viewer/2022020306/555c42d9d8b42a2c068b4ea6/html5/thumbnails/10.jpg)
http://msdn.microsoft.com/en-us/library/ff558947%28v=vs.85%29.aspx
![Page 11: Finding Memory Leaks with UMDH](https://reader031.vdocuments.us/reader031/viewer/2022020306/555c42d9d8b42a2c068b4ea6/html5/thumbnails/11.jpg)