如何确定的对象的大小,C#? [英] How to determine size of an object, c#?

查看:110
本文介绍了如何确定的对象的大小,C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

public class MyClass
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public double Amount { get; set; }
    } 

当我试图找出这个类的大小在64位系统上通过使用WinDbg中,我得到了大小40
对此我无法理解,就我看过MyClass的应有

When I try to find out size of this class on a 64 bit system by using WinDbg I get the size 40 which I am not able to understand, as far as I have read MyClass should have

8 bytes for SyncBlock
8 bytes for TypeHandle
8 bytes for string reference
4 bytes for Int32
8 bytes for double

= 36 bytes

我没有10美誉,这就是为什么我不能够张贴的形象。
任何人有任何想法,为什么WinDbg的是显示4个额外的字节?

I don't have 10 reputation that's why i am not able to post image. Anyone has any idea why WinDbg is showing 4 extra bytes ?

推荐答案

我相信你所看到的是事情需要对齐到8字节边界的64位构建(4字节边界的32位版本)的效果。 40是最接近大小> = 36是一个8字节的边界上。这些链接谈对象大小:

I believe what you're seeing is the effect of things needing to align to 8 byte boundaries in 64 bit builds (and 4 byte boundaries in 32 bit builds). 40 is the closest size >= 36 that is on an 8 byte boundary. These links talk about object size:

内存和字符串(乔恩斯基特的博客)

标杆C#struct和对象的尺寸

钻成的.NET Framework内幕处见CLR如何创建运行时对象

多少内存一个C#字符串占用

这篇关于如何确定的对象的大小,C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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