.NET对象大小对象、大小、NET

2023-09-04 00:56:40 作者:我没挽留你没回头

什么是堆分配对象在.NET中,包括管理开销的大小?我假定对象是沿4字节边界分配的,或者是使用了不同的方法?

What is the size of a heap-allocated Object in .net, including management overhead? I'm assuming Objects are allocated along 4-byte boundaries, or is a different approach used?

推荐答案

4字节边界。可能8字节边界在x64。

4 byte boundaries on x86. Possibly 8 byte boundaries on x64.

有基于x86的8个字节的开销,一类基准和一个同步块。我也不会感到惊讶地发现,12个或16个字节在x64。

There's an 8 byte overhead on x86, for a type reference and a sync block. I wouldn't be surprised to find that's 12 or 16 bytes on x64.

由于某些原因,在x86刚 System.Object的似乎需要12个字节的一个实例,使12个字节的绝对最小容量 - 而是一个类的< $ C C> INT 的也的需要12个字节。我不知道为什么是这样的情况。

For some reason, on x86 an instance of just System.Object appears to take 12 bytes, making 12 bytes the absolute minimum size possible - but a class with an int also takes 12 bytes. I've no idea why this is the case.