Understanding CLR object size between 32 bit vs 64 bit

后端 未结 4 854
旧时难觅i
旧时难觅i 2020-12-14 09:44

I am trying to understand the object size difference between 32 bit and 64 bit processors. Let’s say I have a simple class

class MyClass   
{  
    int x;          


        
4条回答
  •  半阙折子戏
    2020-12-14 10:19

    The CLR is free to lay out objects in memory as it sees fit. It's an implementation detail. You should not rely on any specific layout.

    The difference you see is due to the missing TypeHandle field which is also a part of the CLR object header. Additionally, the fields may be aligned to byte boundaries.


    From Advanced .Net Debugging - CLR Object’s Internal Structure:

    An object’s CLR internal structure is:

    [DWORD: SyncBlock][DWORD: MethodTable Pointer][DWORD: Reference type pointer]…[Value of Value Type field]…

    Object Header: [DWORD: SyncBlock]
    Object Pointer: [DWORD: MethodTable Pointer][DWORD: Reference type pointer]…[Value of Value Type field]…

    Every Object is preceded by an ObjHeader (at a negative offset). The ObjHeader has an index to a SyncBlock.


    So your object is likely laid out like this:

    x86: (aligned to 8 bytes)

      Syncblk     TypeHandle       X            Y
    ------------,------------|------------,------------|
                             8                         16
    

    x64: (aligned to 8 bytes)

             Syncblk                  TypeHandle             X            Y
    -------------------------|-------------------------|------------,------------|
                             8                         16                        24
    

    See also: Drill Into .NET Framework Internals to See How the CLR Creates Runtime Objects

提交回复
热议问题