Вопрос

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.

Это было полезно?

Решение

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

Другие советы

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top