我们收到了来自客户的本机(完整)故障转储文件。在 Visual Studio (2005) 调试器中打开它显示我们遇到了由试图分配 ~10MB 块的 realloc 调用引起的崩溃。转储文件异常大(1,5 GB - 通常它们更像 500 MB)。
因此,我们得出结论,我们有一个内存“泄漏”或失控分配,它们要么完全耗尽了进程的内存,要么至少将其碎片化到足以使 realloc 失败。(请注意,此 realloc 用于分配日志缓冲区的操作,我们并不惊讶它在这里失败,因为除了一些非常大的不可更改的缓冲区之外,一次性 10MB 将是我们所做的更大的分配之一——问题本身可能与此特定分配无关。)
编辑:在下面与 Lex Li 进行评论交流后,我应该补充:这对我们来说是不可复制的(目前)。这只是一个客户转储清楚地显示失控的内存消耗。
主要问题:
现在我们有了一个转储文件,但是我们如何才能找到导致内存使用过多的原因呢?
到目前为止我们所做的:
我们使用了DebugDiag 工具来分析转储文件(所谓的内存压力分析器),这是我们得到的:
Report for DumpFM...dmp
Virtual Memory Summary
----------------------
Size of largest free VM block 62,23 MBytes
Free memory fragmentation 81,30%
Free Memory 332,87 MBytes (16,25% of Total Memory)
Reserved Memory 0 Bytes (0,00% of Total Memory)
Committed Memory 1,67 GBytes (83,75% of Total Memory)
Total Memory 2,00 GBytes
Largest free block at 0x00000000`04bc4000
Loaded Module Summary
---------------------
Number of Modules 114 Modules
Total reserved memory 0 Bytes
Total committed memory 3,33 MBytes
Thread Summary
--------------
Number of Threads 56 Thread(s)
Total reserved memory 0 Bytes
Total committed memory 652,00 KBytes
这只是为了获得一些背景信息。我认为更有趣的是:
Heap Summary
------------
Number of heaps 26 Heaps
Total reserved memory 1,64 GBytes
Total committed memory 1,61 GBytes
Top 10 heaps by reserved memory
-------------------------------
0x01040000 1,55 GBytes
0x00150000 64,06 MBytes
0x010d0000 15,31 MBytes
...
Top 10 heaps by committed memory
--------------------------------
0x01040000 1,54 GBytes
0x00150000 55,17 MBytes
0x010d0000 6,25 MBytes
...
现在,看看堆0x01040000
(1,5 GB),我们看到:
Heap 5 - 0x01040000
-------------------
Heap Name msvcr80!_crtheap
Heap Description This heap is used by msvcr80
Reserved memory 1,55 GBytes
Committed memory 1,54 GBytes (99,46% of reserved)
Uncommitted memory 8,61 MBytes (0,54% of reserved)
Number of heap segments 39 segments
Number of uncommitted ranges 41 range(s)
Size of largest uncommitted range 8,33 MBytes
Calculated heap fragmentation 3,27%
Segment Information
-------------------
Base Address | Reserved Size | Committed Size | Uncommitted Size | Number of uncommitted ranges | Largest uncommitted block | Calculated heap fragmentation
0x01040640 64,00 KBytes 64,00 KBytes 0 Bytes 0 0 Bytes 0,00%
0x01350000 1.024,00 KBytes 1.024,00 KBytes 0 Bytes 0 0 Bytes 0,00%
0x02850000 2,00 MBytes 2,00 MBytes 0 Bytes 0 0 Bytes 0,00%
...
这个段信息到底是什么?
查看列出的分配:
Top 5 allocations by size
-------------------------
Allocation Size - 336 1,18 GBytes
Allocation Size - 1120004 121,77 MBytes
...
Top 5 allocations by count
--------------------------
Allocation Size - 336 3760923 allocation(s)
Allocation Size - 32 1223794 allocation(s)
...
我们可以看到,显然 MSVCR80 堆在 336 字节处拥有 3.760.923 个分配。这很清楚我们用大量的小分配清理了我们的内存,但是我们如何才能获得更多关于这些分配来自哪里的信息呢?
如果我们能以某种方式对这些分配地址中的一些进行采样,然后检查这些地址在进程映像中的哪些位置被使用,那么——假设这些分配的很大一部分是造成我们的“泄漏”的原因——我们也许可以找出在哪里这些失控的分配来自。
不幸的是,我现在真的不知道如何从转储中获取更多信息。
我如何检查这个堆以查看一些“336”分配地址?
如何在转储中搜索这些地址,然后如何找出转储中的哪个指针变量(如果有)保存在这些地址上?
有关使用 DebugDiag、WinDbg 或任何其他工具的任何提示都可以真正提供帮助!另外,如果您不同意我上面的任何分析,请告诉我们!谢谢!