C# Object Size Overhead

前端 未结 3 662
日久生厌
日久生厌 2021-02-04 14:59

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

The C# object consumes as ma

3条回答
  •  -上瘾入骨i
    2021-02-04 15:29

    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.

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

    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.

提交回复
热议问题