计算对象的大小,使其包括其所有子项和(孙子链)的大小 [英] Calculate size of object such that it include size of all its children and (grand children chain)

查看:23
本文介绍了计算对象的大小,使其包括其所有子项和(孙子链)的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设类 A 包含 B 和 C 的实例.B 包含 D、E 和 F,而 C 包含 G、H 和 I 的实例.因此在计算 A 的大小时,我想包括其所有的大小及其子项.当我使用 !dumpheap -stats 命令时,A 的大小似乎并不包括所有包含它的孩子、孙子、孙子.

有什么办法可以在windbg中以这种方式获得A的大小吗?

解决方案

我认为

!objsize <对象地址>

就是你要找的.

但是,它仅适用于单个对象(!dumpheap -stat 汇总所有对象,但不包括所有对象).如果您想为该类型的所有对象执行此操作,则需要 !dumpheap -short -type 和一个循环.

要解决 Marc Sherman 的评论:

<块引用>

根据文档 !objsize 获取父对象及其子对象的大小,它没有提到孙子对象和其他对象:ObjSize 命令包括除父对象之外的所有子对象的大小."

!dumpheap 不考虑孩子:

0:006>!dumpheap -mt 02b24dfc地址 MT 大小02e92410 02b24dfc 2802e9242c 02b24dfc 2802e92474 02b24dfc 28[...]

但是 !objsize 可以:

0:006>!objsize 02e92410sizeof(02e92410) = 28 (0x1c) 字节 (ObjSizeChildren.Object)0:006>!objsize 02e9242csizeof(02e9242c) = 72 (0x48) 字节 (ObjSizeChildren.Object)0:006>!objsize 02e92474sizeof(02e92474) = 160 (0xa0) 字节 (ObjSizeChildren.Object)

使用此代码检查:

class 程序{静态无效主(){var o1 = new Object();var o2 = new Object {child = new Child()};var o3 = new Object {child = new Child {grandChild = new GrandChild()}};Console.WriteLine(立即调试");Console.ReadLine();Console.Write(o1);Console.Write(o2);Console.Write(o3);}}类对象{私长一;私人长 b;公共儿童;}内部类 Child{私长一;私人长 b;私人长c;私人长d;public GrandChild 孙子;}内部类 GrandChild{私长一;私人长 b;私人长c;私人长d;私长e;私人长f;私人长g;私长h;私人长我;私人长j;}

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?

解决方案

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;
}

这篇关于计算对象的大小,使其包括其所有子项和(孙子链)的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆