安裝完WinDbug之後,在WinDbug路徑下會有一個UMDH.exe的檔案,通過這個檔案可以通過“棧回溯資料庫”的方式定位應用程式記憶體泄露的地方。
The User-Mode Dump Heap (UMDH) tool, Umdh.exe, analyzes the Microsoft Windows heap memory allocations for a given process. UMDH has the following modes.
- Analyze a running process ("Mode 1"). UMDH captures and analyzes the heap memory allocations for a process. For each allocation, UMDH displays the size of the allocation, the size of the overhead, the pointer
to the allocation and the allocation stack. If a process has more than one active memory heap, UMDH captures all heaps.
- Analyze UMDH log files ("Mode 2"). UMDH analyzes the logs to help detect memory leaks. When comparing two UMDH output logs from subsequent runs of a UMDF driver, UMDH displays the calls in which the allocation
size increased the most between the runs.
umdh只能針對分配了記憶體,但是沒有進行釋放的程式起到檢測作用。除了umdh.exe 通過!heap 也可找到增長的那個堆,並找到對應的棧資訊,從而定位到函數源碼。
A:UMDH方式
建立應用程式WinDbugTest.exe
1.cmd視窗轉到gflags.exe目錄(WinDbug路徑),運行:gflags.exe -i F:\Test|WinDbugTest\\Debug\WinDbugTest.exe +ust 來啟用目標程式的ust功能
2.開啟應用程式WinDbugTest.exe不執行任何操作;
3.cmd視窗轉到umdh.exe目錄(WinDbug路徑),運行:umdh.exe -pn:WinDbugTest.exe -f:u1.log 記錄當前棧引用資料庫相關資訊
4.對程式進行相關操作,執行一些建立記憶體以及釋放記憶體的操作。
5.cmd視窗轉到umdh.exe目錄(WinDbug路徑),運行:umdh.exe -pn:WinDbugTest.exe -f:u2.log 記錄當前棧引用資料庫相關資訊
6.cmd視窗轉到umdh.exe目錄(WinDbug路徑),運行:umdh.exe u1.log u2.log -f:u3.log 將之前擷取的棧引用資料庫資訊進行對比來找到出現異常的地址
B:!Heap方式
1.!heap -s
2.!heap -stat -h XXXXX
3.!heap -flt s XX
4.!heap -p -a VVV