分配在堆对象分配、对象

2023-09-03 01:54:51 作者:无人生还

每当创建任何新的对象时,该对象上堆创建。分配给每个对象的内存有两个附加字段1)类型对象的指针2)同步块索引。

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 index.

究竟是这两个领域的使用。任何人都可以阐明这个角度?

What exactly is the usage of these two fields. Can anybody shed light on this?

推荐答案

该类型的对象指针用来重新present对象的类型。这是必需的:

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

方法查找(虚函数表) 检查蒙上 查找键入对象,如果你调用的GetType 。 Method lookup (the vtable) Checking casts Finding the Type object if you call GetType.

的同步块字段主要用于锁定。它不仅填补时,它需要的,当锁始终是无可争议的CLR使得做它不需要任何外部数据瘦的锁。否则,它是在一个进程范围表中的条目 - 我不知道什么是在表中的细节,但我想这是像等待对象的监视器线程的列表。当然的信息的最重要的位是锁是否被当前保持,通过该螺纹,以及其计数(由于.NET锁折返性质)。

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

该同步块也填补了,如果你调用 GetHash code(),它不是重写 - 它使用过程全表分配一个稳定的数量,基本上。 (该对象的地址是不够好,因为它可以随时间改变。)

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