Get-DbgClrHeap

Equivilent to !dumpheap

Provides information on the contents of the managed heap. Each type on the heap (or matching type if a -type filter specified) is returned as an object with properties for the MethodTable (MT), Count, ClassName etc...

PowerDbg > $types = Get-DbgClrHeap -type Dictionary
PowerDbg > $types

MT               Count ClassName
--               ----- ---------
000007ff00054a70 1     System.Collections.Generic.Dictionary`2[[System.Strin...
000007fee877c290 1     System.Collections.Generic.Dictionary`2[[System.Strin...
000007ff00055f60 1     System.Collections.Generic.Dictionary`2+Entry[[System...
000007fee877cb28 2     System.Collections.Generic.Dictionary`2+Entry[[System...


Each type returned has an Instances property containing the addresses of all the instances of that type:

PowerDbg > $types[3].Instances | format-table -AutoSize

Address          MT               Size ClassName
-------          --               ---- ---------
0000000002716d98 000007fee877cb28 120  System.Collections.Generic.Dictionary`2+Entry[[System.String, mscorlib],[Syst...
0000000002732d58 000007fee877cb28 248  System.Collections.Generic.Dictionary`2+Entry[[System.String, mscorlib],[Syst...


The entries within the Instances list can be piped directly to Get-DbgObject, or passed as the parameter to Get-DbgDictionaryContents

PowerDbg > $types[3].Instances | Get-DbgObject

__Fields      : {}
__Name        : System.Collections.Generic.Dictionary`2+Entry[[System.String, mscorlib],[System.Resources.ResourceLocat
                or, mscorlib]][]
__MethodTable : 000007fee877cb28
__EEClass     : 000007fee8421650
__Size        : 120(0x78) bytes
__Array       : Rank 1, Number of elements 3, Type VALUETYPE

__Fields      : {}
__Name        : System.Collections.Generic.Dictionary`2+Entry[[System.String, mscorlib],[System.Resources.ResourceLocat
                or, mscorlib]][]
__MethodTable : 000007fee877cb28
__EEClass     : 000007fee8421650
__Size        : 248(0xf8) bytes
__Array       : Rank 1, Number of elements 7, Type VALUETYPE

Finding the Right Type

Since the -type parameter is a simple string match, you will often get back a list of more types than you expect. In the example above, matching on Dictionary also brought back the internal helper classes that a Dictionary object uses.

The simplest way of locating exactly the type you are looking for is, unfortunately, to just eyeball the items after piping to Format-List. If you need to do so programatically, crafting the right regex to further constrain the set based on the ClassName property is possible. In the following example I match a dictionary with a particular generic param, and I explicitly exclude the nested helper class:

PowerDbg > $type = $types | ? { ($_.ClassName -match 'AData') -and -not ($_.ClassName -match '\+Entry') }
PowerDbg > $type

MT         : 000007ff00054a70
Count      : 1
TotalSize  : 88
Class Name : System.Collections.Generic.Dictionary`2[[System.String, mscorlib],
             [ManagedScenarios.ADataStructure, ManagedScenarios]]
Instances  : {System.Object}
ClassName  : System.Collections.Generic.Dictionary`2[[System.String, mscorlib],
             [ManagedScenarios.ADataStructure, ManagedScenarios]]


In a future release we may well expose the generic parameters as properties on the type object, which would make filtering much easier.

Last edited Nov 15, 2010 at 1:51 PM by piers7, version 1

Comments

No comments yet.