您的位置:首页 > 其它

【性能分析】UMDH分析内存泄露

2017-06-22 21:03 204 查看
在使用WPA分析性能的时候,发现一款轻量级的内存泄露分析工具UMDH(User-Mode Dump Heap),现在研究实践如何使用该工具进行内存泄露分析

Umdh 是 Debugging Tools for Windows 里面的一个工具,主要通过分析比较进程的Heap Stack trace信息来发现内存泄露

HMDH内存泄露分析适用范围

dll内存泄露

长时间才出现的

不易分析的内存泄露问题

使用前先要进行安装和配置

下载

http://www.microsoft.com/whdc/devtools/debugging/default.mspx

设置环境变量

使用命令行进行配置

set _NT_SYMBOL_PATH=F:\Windows Kits\Debuggers\x64


使用界面进行配置



设置检测模式

可以在命令行中输入
gflags
或者启动程序Debugging Tools for Windows,启动后设置界面如下



现在进行界面设置

System Registry



Kernel Flags



umdh创建heap快照

开启cmd,输入命令
umdh
,界面提示如下



我们使用提示中的EXAMPLE

EXAMPLE:

-1- umdh.exe -pn:application_name.exe -f:FirstDump.txt
-2- ... exercise the application
-3- umdh.exe -pn:application_name.exe -f:SecondDump.txt
-4- umdh.exe FirstDump.txt SecondDump.txt -f:Result.txt
Compares allocations from the two dumps.

umdh.exe Dump.txt
Investigate a single dump.


内存泄露的.exe程序为博客 【性能分析】内存泄露C++程序 编写的C++小程序 newChar.exe

生成第一个时间节点分析文件

umdh  -pn:newChar.exe -f:D:/FirstDump.txt


等待一段时间,newChar.exe运行ing

生成第二个时间节点分析文件

umdh -pn:newChar.exe -f:D:/SecondDump.txt


对比数据

将两次文件综合得到最终分析文件

umdh D:/FirstDump.txt D:/SecondDump.txt -f:D:/Result.txt


打开生成的Result.txt 文件

+11e1a118 ( 13e509b4 - 203689c)   13e1 allocs   BackTraceC3D91172
+    11de (  13e1 -   203)  BackTraceC3D91172   allocations

ntdll!RtlWalkHeap+203
ntdll!memset+DA3A
MSVCR120D!heapwalk+27D
MSVCR120D!free_dbg+9AB
MSVCR120D!msize_dbg+309
MSVCR120D!msize_dbg+299
MSVCR120D!malloc+2A
MSVCR120D!operator new+13
newChar!operator new[]+13 (f:\dd\vctools\crt\crtw32\stdcpp\newaop.cpp, 7)
newChar!wmain+32 (d:\c++project\newchar\newchar\newchar.cpp, 15)
newChar!__tmainCRTStartup+19D (f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c, 623)
newChar!wmainCRTStartup+E (f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c, 466)
KERNEL32!BaseThreadInitThunk+22
ntdll!RtlUserThreadStart+34

-      78 (     0 -    78)      0 allocs    BackTraceC5705F12
-       3 (     0 -     3)  BackTraceC5705F12   allocations

ntdll!RtlWalkHeap+203
ntdll!memset+DA3A
ntdll!RtlAllocateActivationContextStack+27
ntdll!LdrShutdownThread+300
ntdll!LdrInitializeThunk+EB
ntdll!LdrInitializeThunk+E

-     198 (     0 -   198)      0 allocs    BackTraceC5705F92
-       3 (     0 -     3)  BackTraceC5705F92   allocations

ntdll!RtlWalkHeap+203
ntdll!memset+DA3A
ntdll!RtlAdjustPrivilege+140
ntdll!EtwEventRegister+EE
KERNEL32!BaseThreadInitThunk+22
ntdll!RtlUserThreadStart+34

-     ab0 (     0 -   ab0)      0 allocs    BackTraceC5706152
-       3 (     0 -     3)  BackTraceC5706152   allocations

ntdll!RtlWalkHeap+203
ntdll!memset+DA3A
ntdll!EtwEventRegister+AF7
KERNEL32!BaseThreadInitThunk+22
ntdll!RtlUserThreadStart+34

Total increase == 11e19458 requested +  310a8 overhead = 11e4a500


每一段代表一次系统分配内存的动作,每一段最上为系统内核最终调用分配函数



比如ntdll!RtlWalkHeap+203 ,最下则是应用层调用,并注明了在哪个文件的具体哪一行。除了一些系统的函数分配动作,剩下的都是由程序产生,故比较容易可分析出哪里出现了内存泄露

分析数据

让我们分析一下
BackTraceC3D91172
模块数据

找到第二个日志文件,即
SecondDump.txt
文件,搜索
BackTraceC3D91172




再查看
Result.txt
文件

+11e1a118 ( 13e509b4 - 203689c)   13e1 allocs   BackTraceC3D91172
+    11de (  13e1 -   203)  BackTraceC3D91172   allocations

ntdll!RtlWalkHeap+203
ntdll!memset+DA3A
MSVCR120D!heapwalk+27D
MSVCR120D!free_dbg+9AB
MSVCR120D!msize_dbg+309
MSVCR120D!msize_dbg+299
MSVCR120D!malloc+2A
MSVCR120D!operator new+13
newChar!operator new[]+13 (f:\dd\vctools\crt\crtw32\stdcpp\newaop.cpp, 7)
newChar!wmain+32 (d:\c++project\newchar\newchar\newchar.cpp, 15)
newChar!__tmainCRTStartup+19D (f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c, 623)
newChar!wmainCRTStartup+E (f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c, 466)
KERNEL32!BaseThreadInitThunk+22
ntdll!RtlUserThreadStart+34


发现
wmain
函数中有new操作,分配了新的内存,但是并没有进行释放,定位到源代码行



即可定位到代码中内存泄露的代码行,搞定
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息