.NET 中类(对象)的大小 [英] Size of A Class (object) in .NET

查看:27
本文介绍了.NET 中类(对象)的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET 中如何判断一个类是大还是小?它是根据它的属性或字段的数量,其属性/字段的数据类型来衡量的吗?或返回类型的方法?它的方法的参数?其方法的访问修饰符,虚方法?谢谢..

How to determine if a Class in .NET is big or small? Is it measured on how many it's attributes or fields, datatype of its attributes/fields? or return type of methods? parameters of it's methods? access modifier of its methods, virtual methods? thanks..

 class A
{

  string x { get; set; }
}

class B 
{
  int x { get; set; }
}

在这个例子中,如果我像这样实例化类 A 和 B

in this example if I instantiate class A and B like this

 A objA = new A();
 B objB = new B();

类 objA 是否更大,因为它拥有一个 String 属性而 objB 只拥有一个 Int?虽然我没有为它的属性设置任何值.谢谢

Is class objA the bigger one because it holds an String property and objB holds only an Int? although I didn't set any value to it's property. thanks

只是为了澄清我的问题

假设我有一堂课

public class Member
{
    public string MainEmpId { get; set; }
    public string EmpId { get; set; }
}

和另一个班

public class User
{
    public string AccessLevel { get; set; }
    public string DateActivated { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Mi { get; set; }
    public string Password { get; set; }
    public string UserId { get; set; }
    public string UserName { get; set; }
    public string Active { get; set; }
    public string ProviderName { get; set; }        
    public string ContactPerson { get; set; }
    public string Relation { get; set; }
    public string Landline { get; set; }
    public string MobileNo { get; set; }
    public string Complaint { get; set; }
    public string Remarks { get; set; }
    public string Reason { get; set; }
    public string RoomType { get; set; }
}

如果我像这样实例化它

  Member A = new Member();
  User B = new User()

物体A比物体B大吗?我知道这是一个奇怪的问题,但我相信对象的每一个实例都会占用内存空间..

is the object A larger than object B? I know it's an odd question but I believe every intantiation of an object eats memory space..

推荐答案

类实例的大小由以下因素决定:

The size of a class instance is determined by:

  • 实例中实际存储的数据量
  • 值之间所需的填充
  • 内存管理使用的一些额外的内部数据

因此,通常需要包含字符串属性的类(在 32 位系统上):

So, typically a class containing a string property needs (on a 32 bit system):

  • 8 个字节用于内部数据
  • 4 个字节用于字符串引用
  • 4 字节的未使用空间(达到内存管理器可以处理的最小 16 字节)

通常一个包含整数属性的类需要:

And typically a class containing an integer property needs:

  • 8 个字节用于内部数据
  • 4 个字节的整数值
  • 4 字节的未使用空间(达到内存管理器可以处理的最小 16 字节)

如您所见,字符串和整数属性在类中占用相同的空间,因此在您的第一个示例中,它们将使用相同的内存量.

As you see, the string and integer properties take up the same space in the class, so in your first example they will use the same amount of memory.

string 属性的值当然是另一回事,因为它可能指向堆上的一个字符串对象,但那是一个单独的对象,而不是指向它的类的一部分.

The value of the string property is of course a different matter, as it might point to a string object on the heap, but that is a separate object and not part of the class pointing to it.

对于更复杂的类,填充开始起作用.例如,包含布尔值和字符串属性的类将使用:

For more complicated classes, padding comes into play. A class containing a boolean and a string property would for example use:

  • 8 个字节用于内部数据
  • 1 个字节表示布尔值
  • 3 个字节的填充以达到 4 字节的边界
  • 4 个字节用于字符串引用

请注意,这些是类的内存布局示例.确切的布局取决于框架的版本、CLR 的实现以及它是 32 位还是 64 位应用程序.由于程序可以在 32 位或 64 位系统上运行,因此编译器甚至不知道内存布局,而是在执行之前决定代码何时进行 JIT:ed.

Note that these are examples of memory layouts for classes. The exact layout varies depending on the version of the framework, the implementation of the CLR, and whether it's a 32-bit or 64-bit application. As a program can be run on either a 32-bit or 64-bit system, the memory layout is not even known to the compiler, it's decided when the code is JIT:ed before execution.

这篇关于.NET 中类(对象)的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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