Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLR Sync Block Address

When I do:

public class Employee
{
    public int exp;
}

class Program
{
    static void Main(string[] args)
    {            
        Employee o1 = new Employee();
        o1.exp = 3;
        lock (o1)
        {
            //I am here
        }
    }
}

and get memory of o1 (address is 0x022cf940):

enter image description here

I realized couple of things mentioned below:

  1. Green rectangle is sync block, which is 12
  2. Blue rectangle is 4 byte address of type
  3. Red rectangle is 4 byte integer, which is 3;

Question: Where is the space of sync blocks and how can I find it? What does "12" stand for?

like image 222
zzfima Avatar asked Jun 07 '15 06:06

zzfima


1 Answers

The sync block has more than one use. It can store the value of Object.GetHashCode() so that an object always returns the same hash code when GetHashCode() is called again. It can store the ID of the owner thread of a lock statement. It has several dedicated bits, like indicating that the finalizer for an object has been run. And it can store a handle to an allocated sync block, necessary when a thread both called GetHashCode and used lock and the info can't fit in the sync block anymore. It is very heavily micro-optimized.

Your case is the simple case, only lock was called and none of the dedicated bits are turned on. So you see the owner of the lock, 0x12 = 18 is the Thread.ManagedThreadId of the thread that owns the lock. That can come in pretty doggone handy when you ever need to troubleshoot a deadlock.

You can make the debugger display a bit easier to interpret when you right-click the window and select "4-byte Integer". The blue rectangle is the method table pointer for the object (aka "type handle"). It indicates the type of the object, Object.GetType() uses it. The red rectangle is where the object starts storing its fields. Since yours only has the exp field and its type is Int32, you can see 3 back.

like image 133
Hans Passant Avatar answered Oct 25 '22 14:10

Hans Passant