What is the ClrMD equivalent to !DumpHeap -live?
<p><em><a href="https://github.com/Microsoft/dotnetsamples/tree/master/Microsoft.Diagnostics.Runtime/CLRMD" rel="nofollow">ClrMD</a></em> is a great tool to inspect a live executable or a memory dump. To dump all managed heap objects as summary you can use</p>

<p><a href="https://blogs.msdn.microsoft.com/dotnet/2013/05/01/net-crash-dump-and-live-process-inspection/" rel="nofollow">https://blogs.msdn.microsoft.com/dotnet/2013/05/01/net-crash-dump-and-live-process-inspection/</a></p>

<pre><code>var stats = from o in heap.EnumerateObjects()
            let t = heap.GetObjectType(o)
            group o by t into g
            let size = g.Sum(o =&gt; (uint)g.Key.GetSize(o))
            orderby size
            select new
            {
                Name = g.Key.Name,
                Size = size,
                Count = g.Count()
            };

foreach (var item in stats)
    Console.WriteLine("{0,12:n0} {1,12:n0} {2}", item.Size, item.Count, item.Name);
</code></pre>

<p>That works great and is the equivalent of <em>!DumpHeap -stat</em> in <em>Windbg</em> with the <em>SOS</em> Extension. For regression testing it is often desireable to check after a test if objects were leaked. That looks like a perfect fit for the code above but unfortunately it produces false positives because there may be objects which are no longer rooted but they are still reported in the summary. That can lead to reported leaks although there are none. </p>

<p><em>SOS</em> has mitigated this issue by addine to <em>!DumpHeap</em> the <em>-live</em> switch. What would be the equivalent <em>ClrMD</em> code to get only the live objects so regression tests only fail for a real reason? </p>

<p>I guess I need to use <em>ClrType.EnumerateRefsOfObjectCarefully</em> to walk the stack recursively until I find a root object (<em>ClrHeap.EnumerateRoots</em>) but that approach needs many temporary Hashsets to track recursive object graph. Is this the only way to do it or is there somewhere inside MS already a correct and well performing sample available (<em>PerfView</em> source?).</p>