.net - Understanding CLR object size between 32 bit vs 64 bit -


i trying understand object size difference between 32 bit , 64 bit processors. let’s have simple class

class myclass    {       int x;       int y;   }   

so on 32 bit machine, integer 4 bytes. if add syncblock ( 4 bytes), object size 12 bytes. why showing 16 bytes?

 0:000> !do 0x029d8b98   name: consoleapplication1.program+myclass   methodtable: 000e33b0   eeclass: 000e149c   size: 16(0x10) bytes    (c:\mytemp\consoleapplication1\consoleapplication1\bin\x86\debug\consoleapplication1.exe)   fields:         mt    field   offset                 type vt     attr    value name   71972d70  4000003        4         system.int32  1 instance        0 x   71972d70  4000004        8         system.int32  1 instance        0 y   

on 64 bit machine, integer still 4 bytes thing changed syncblock 8 bytes ( pointers 8 bytes on 64 bit machines). mean object size 16 bytes. why showing 24 bytes?

 0:000> !do 0x00000000028f3c90   name: consoleapplication1.program+myclass   methodtable: 000007ff00043af8   eeclass: 000007ff00182408   size: 24(0x18) bytes    (c:\mytemp\consoleapplication1\consoleapplication1\bin\debug\consoleapplication1.exe)   fields:                 mt    field   offset                 type vt     attr            value name   000007fef4edd998  4000003        8         system.int32  1 instance                0 x   000007fef4edd998  4000004        c         system.int32  1 instance                0 y   

the clr free lay out objects in memory sees fit. it's implementation detail. should not rely on specific layout.

the difference see due missing typehandle field part of clr object header. additionally, fields may aligned 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 preceded objheader (at negative offset). objheader has index syncblock.


so object laid out this:

x86: (aligned 8 bytes)

   syncblk     typehandle       x            y ------------,------------|------------,------------|                          8                         16 

x64: (aligned 8 bytes)

          syncblk                  typehandle             x            y -------------------------|-------------------------|------------,------------|                          8                         16                        24 

see also: drill .net framework internals see how clr creates runtime objects


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -