C# Object Size Overhead

前端 未结 3 661
日久生厌
日久生厌 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条回答
  •  误落风尘
    2021-02-04 15:12

    There are two types of overhead for an object:

    • Internal data used to handle the object.
    • Padding between data members.

    The internal data is two pointers, so in a 32-bit application that is 8 bytes, and in a 64-bit application that is 16 bytes.

    Data members are padded so that they start on an even address boundary. If you for example have a byte and an int in the class, the byte is probably padded with three unused bytes so that the int starts on the next machine word boundary.

    The layout of the classes is determined by the JIT compiler depending on the architecture of the system (and might vary between framework versions), so it's not known to the C# compiler.

提交回复
热议问题