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

来源:互联网 发布:java类的定义格式 编辑:程序博客网 时间:2024/05/17 20:14

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

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

HMDH内存泄露分析适用范围

  1. dll内存泄露
  2. 长时间才出现的
  3. 不易分析的内存泄露问题

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

下载

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+34Total 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操作,分配了新的内存,但是并没有进行释放,定位到源代码行

这里写图片描述

即可定位到代码中内存泄露的代码行,搞定