CLR Sync Block Address

别来无恙 提交于 2019-11-30 13:08:15

问题


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):

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?


回答1:


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.



来源:https://stackoverflow.com/questions/30690790/clr-sync-block-address

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!