Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object Size in SOS Debugging C#.Net

Tags:

c#

debugging

sos

class Book
{
    public int ISBN { get; set; }      
}

void Method() 
{
  Book book = new Book(); 
  // Break and verify in SoS Debugging.
}


   !dumpheap -type Book
   PDB symbol for clr.dll not loaded
   Address       MT     **Size**
   00c6b76c 009b7f2c       **12**      
   total 0 objects
   Statistics:
   MT    Count    TotalSize Class Name
   009b7f2c        1           12 GCTest.Book
   Total 1 objects

How the size of object here is 12 bytes . It contains only one integer property. sizeof(int) = 4 bytes remaining 8 bytes ? (object instantiation). Can anyone shed some light.

like image 241
C-va Avatar asked May 11 '26 23:05

C-va


2 Answers

Every reference object has two extra field appended :

Object type ptr : 4 bytes (ddress of a memory (AppDomain specific) that contains a structure holding the Method Table of the Reference Type for which the object is instantiated or points to)

Sync block adress : 4 bytes (sync block address and points to a location in a process-wide table that contains structures used for synchronizing access to instances of Reference Types)

More info Check the memory layout in this article

like image 78
MBen Avatar answered May 13 '26 12:05

MBen


You have on top of the object strucure 8 bytes of information:

4 bytes for object reference

4 bytes for syncblk: a special memory block used for syncronisation. In your specifica case it will be set to 0, as your object doesn't partecipate in any sync operaiton.

In other words:

<--SyncBlock(4bytes)--> <--ObjectReferece(4bytes)--> <--your object data-->

For more information have a look at:

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

like image 42
Tigran Avatar answered May 13 '26 12:05

Tigran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!