C#对象大小开销 [英] C# Object Size Overhead

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

问题描述

可能重复:结果
如何获得对象大小的内存?结果
在C#中

我的工作中消耗内存的应用优化。关于我有问题关于C#引用类型的大小开销。

I am working on optimization of memory consuming application. In relation to that I have question regarding C# reference type size overhead.

C#的对象占用的字节数它的领域,再加上一些的额外的管理
开销
。我猜想管理开销可针对不同的.NET版本,并实现不同。

The C# object consumes as many bytes as its fields, plus some additional administrative overhead. I presume that administrative overhead can be different for different .NET versions and implementations.

你知道什么是大小(或最大尺寸,如果开销变量)的C#对象(C#4.0和管理开销 Windows 7和8的环境)?

Do you know what is the size (or maximum size if the overhead is variable) of the administrative overhead for C# objects (C# 4.0 and Windows 7 and 8 environment)?

请问行政开销,32位或64位.NET运行时之间是不同的?

Does the administrative overhead differs between 32- or 64-bit .NET runtime?

推荐答案

通常情况下,每个由GC分配的对象8或12字节的开销。有4个字节用于syncblk和4个字节用于在32位运行时,在64位运行时间的8个字节的类型手柄。有关详细信息,请参阅钻入.NET框架内幕的对象实例部分看到CLR如何创造运行时对象在MSDN杂志。

Typically, there is an 8 or 12 byte overhead per object allocated by the GC. There are 4 bytes for the syncblk and 4 bytes for the type handle on 32bit runtimes, 8 bytes on 64bit runtimes. For details, see the "ObjectInstance" section of Drill Into .NET Framework Internals to See How the CLR Creates Runtime Objects on MSDN Magazine.

请注意,实际基准并改变32位或64位.NET运行时也是如此。

Note that the actual reference does change on 32bit or 64bit .NET runtimes as well.

此外,可能有填充为类型,以适应在地址界限,尽管这取决于所讨论的类型很多。这可能会导致对象之间的空白空间为好,但要由运行时(主要是,虽然你可以使用的 StructLayoutAttribute ),以确定何时以及如何将数据对齐。

Also, there may be padding for types to fit on address boundaries, though this depends a lot on the type in question. This can cause "empty space" between objects as well, but is up to the runtime (mostly, though you can affect it with StructLayoutAttribute) to determine when and how data is aligned.

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

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