A类(对象)的.NET尺寸 [英] Size of A Class (object) in .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()
是一个比对象B大的对象吗? 我知道这是一个奇怪的问题,但我相信,对象的每个intantiation吃内存空间。
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位的系统上运行,该存储器布局甚至不知道的编译器,它的决定时,code是JIT:执行前编
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.
这篇关于A类(对象)的.NET尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!