Objects allocated on heap

后端 未结 4 572
孤城傲影
孤城傲影 2021-01-02 05:18

Whenever any new object is created, the object is created on heap. The memory allocated for each object has two additional fields 1) The type object pointer 2) sync block in

相关标签:
4条回答
  • 2021-01-02 05:41

    The sync block index is used under the hood by the Monitor class and thus the lock statement also.

    0 讨论(0)
  • 2021-01-02 05:46

    The type object pointer is used to represent the type of the object. This is required for:

    • Method lookup (the vtable)
    • Checking casts
    • Finding the Type object if you call GetType.

    The syncblock field is primarily used for locking. It's only filled in when it needs to be, and when a lock is always uncontested the CLR makes do with a "thin" lock which doesn't require any external data. Otherwise, it's an entry in a process-wide table - I don't know the details of what's in the table, but I would imagine it's things like a list of threads waiting on the object's monitor. Of course the most important bit of information is whether or not the lock is currently held, by which thread, and what its count is (due to the reentrant nature of .NET locks).

    The syncblock is also filled in if you call GetHashCode() and it's not overridden - it uses the process-wide table to allocate a stable number, basically. (The address of the object isn't good enough as it can change over time.)

    0 讨论(0)
  • 2021-01-02 05:56

    Some bits of sync block index are also used by GC to mark an object as garbage in case it is no longer referenced.

    0 讨论(0)
  • 2021-01-02 06:00

    Type object is what returned by obj.GetType call

    sync block used for synchronization

    See:

    • CLR Memory Model Part 1
    • CLR Memory Model Part 2
    • More on locks
    0 讨论(0)
提交回复
热议问题