获得 Visual Studio 终极版“调试管理内存"Windbg 的输出 [英] getting visual studio ultimate "Debug Managed Memory" output from windbg
问题描述
是否可以在 WINDBG 中获取堆上托管对象的摘要信息,类似于 Visual Studio Ultimate 的调试托管内存"选项提供的摘要.
Is it possible to obtain summary information for managed objects on the heap in WINDBG that's similar to the summary presented by Visual Studio Ultimate's 'Debug Managed Memory' option.
我可以获得一些信息,但要具体情况具体分析,而且相当乏味.是否有一个宏或一组命令可以使用 WINDBG 产生类似的输出?
I can obtain some of the information, but it's on a case by case basis, and is quite tedious. Is there a macro or set of commands that can produce similar output using WINDBG?
Visual Studio 似乎有一个简洁的小例程,它收集所有根并显示根对象类及其总内存的摘要.
Visual Studio seems to have a neat little routine where it collects all the roots and shows a summary of the root object classes and their total memory.
推荐答案
对于任何 .NET 问题,您首先需要 SOS 扩展
Like for any .NET issue, you'll first need the SOS extension
.loadby sos clr; .loadby sos mscorwks
然后您可以使用
!dumpheap -stat; * Statistics output, good if you don't know what you're looking for
!dumpheap -type <SubstringOfClass>; * If you know what type you're after
!dumpheap -mt <MethodTable>; * If the class substring is not unique enough
要获得包含大小,您可以使用
To get the inclusive size, you can use
!objsize <address>
要查找对象的根,请使用
To find the root of an object, use
!gcroot <address>
请注意,恕我直言,没有显示所有根对象的便捷方法.
Note that IMHO there is no convenient way of showing all root objects.
我不确定 Visual Studio 是否列出堆栈上的对象.在 WinDbg 中,这将是
I'm not sure if Visual Studio lists objects on the stack. In WinDbg this would be
~*e !dso
示例演练:
0:005> !dumpheap -stat
Statistics:
MT Count TotalSize Class Name
000007fef2611ec8 1 24 System.Collections.Generic.ObjectEqualityComparer`1[[System.Runtime.Serialization.MemberHolder, mscorlib]]
...
000007fef2622968 1 64 System.BaseConfigHandler+CreateAttributeCallback
...
000007fef25fa690 396 117910 System.Byte[]
000007fef25a4458 1570 227560 System.Object[]
000007fef25f6508 3300 234762 System.String
Total 17883 objects
0:005> !dumpheap -stat -type Handler
Statistics:
MT Count TotalSize Class Name
000007fef2622968 1 64 System.BaseConfigHandler+CreateAttributeCallback
000007fef26228b0 1 64 System.BaseConfigHandler+CreateNodeCallback
000007fef26227f8 1 64 System.BaseConfigHandler+ErrorCallback
Total 3 objects
0:005> !dumpheap -mt 000007fef2622968
Address MT Size
0000000002594848 000007fef2622968 64
Statistics:
MT Count TotalSize Class Name
000007fef2622968 1 64 System.BaseConfigHandler+CreateAttributeCallback
Total 1 objects
0:005> !objsize 0000000002594848
sizeof(0000000002594848) = 168728 (0x29318) bytes (System.BaseConfigHandler+CreateAttributeCallback)
0:005> !gcroot 0000000002594848
Found 0 unique roots (run '!GCRoot -all' to see all roots).
0:005> !gcroot -all 0000000002594848
Found 0 roots.
所以这个对象似乎没有更多的引用,并且会在下一次 GC 时被垃圾回收.
So it seems that this object has no more references and will be garbage collected during next GC.
0:001> !dso
OS Thread Id: 0xb2c (1)
RSP/REG Object Name
00000000024B0000 00000000024b1048 System.Exception
这篇关于获得 Visual Studio 终极版“调试管理内存"Windbg 的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!