1

Let's say class A contains instances of B and C. B contains D, E and F whereas C contains instances of G, H and I. So when calculating the size of A, I would like to include size of all of its and its child items. When I use !dumpheap -stats command, the size of A doesn't seem to include all of its containing children, grand-children, grand-grand children.

Is there any way I can get the size of A in such a way within windbg?

crazy novice
  • 1,757
  • 3
  • 14
  • 36

1 Answers1

2

I think

!objsize <object address>

is what you're looking for.

However, it works for single objects only (!dumpheap -stat sums up all objects, but not inclusive). If you want to do it for all objects of that type, you would need !dumpheap -short -type and a loop.

To address the comment of Marc Sherman:

According to the doc !objsize gets the size of the parent and its children, it doesn't mention grand-children and beyond: "The ObjSize command includes the size of all child objects in addition to the parent."

!dumpheap does not consider children:

0:006> !dumpheap -mt 02b24dfc
 Address       MT     Size
02e92410 02b24dfc       28     
02e9242c 02b24dfc       28     
02e92474 02b24dfc       28    
[...]

But !objsize does:

0:006> !objsize 02e92410
sizeof(02e92410) = 28 (0x1c) bytes (ObjSizeChildren.Object)
0:006> !objsize 02e9242c
sizeof(02e9242c) = 72 (0x48) bytes (ObjSizeChildren.Object)
0:006> !objsize 02e92474
sizeof(02e92474) = 160 (0xa0) bytes (ObjSizeChildren.Object)

Checked with this code:

class Program
{
    static void Main()
    {
        var o1 = new Object();
        var o2 = new Object {child = new Child()};
        var o3 = new Object {child = new Child {grandChild = new GrandChild()}};
        Console.WriteLine("Debug now");
        Console.ReadLine();
        Console.Write(o1);
        Console.Write(o2);
        Console.Write(o3);
    }
}

class Object
{
    private long a;
    private long b;
    public Child child;
}

internal class Child
{
    private long a;
    private long b;
    private long c;
    private long d;
    public GrandChild grandChild;
}

internal class GrandChild
{
    private long a;
    private long b;
    private long c;
    private long d;
    private long e;
    private long f;
    private long g;
    private long h;
    private long i;
    private long j;
}
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • According to the doc `!objsize` gets the size of the parent and its children, it doesn't mention grand-children and beyond: "The ObjSize command includes the size of all child objects in addition to the parent." from https://msdn.microsoft.com/en-us/library/bb190764%28v=vs.110%29.aspx – Marc Sherman Mar 09 '15 at 13:49
  • Holy guacamole! Thomas did you just update 3 days ago? If so, thank you *but* maybe an MRE with windbg and needed debugger extension versions from early 2015 are needed? Just kidding! Kind of ;-) – Marc Sherman Jul 08 '21 at 21:04