Does a value type keep Type pointer + Sync root +

2019-04-10 11:26发布

问题:

Does a value type keep Type pointer + Sync root + Static fields like a reference type? This question is an extended version of the following one: do-value-types-have-type-objects. Can anyone clarify:

  • Do value types have a related System.Type-object stored in CLR heap?
  • Where value type static fields and methods are stored if there is no associated type object?
  • Do value types have sync root field (are value types thread safe if there is no sync root block for them)?

回答1:

Do value types have a related type-object stored in CLR heap?

No, There isn't. Structs don't have header associated with it and no type information is stored along with it. If you ask about the System.Type, yes the Type metadata will be in heap. But it won't be created upfront.

Where value type static fields are stored if there is no associated type object in thread stacks?

Irrespective of ValueType or ReferenceType, static fields are stored in special heap called "High Frequency Heap" which you have one per AppDomain. Unlike the "Garbage Collected Heap", this heap is not garbage collected.

Every static variable is stored on the heap, regardless of whether it's declared within a reference type or a value type. There is only one slot in total no matter how many instances are created. (There don't need to be any instances created for that one slot to exist though.) Note that this heap is separate from the normal garbage collected heap - it's known as a "high frequency heap", and there's one per application domain.

Above quote by Jon Skeet

Do value types have sync root field (are value types thread safe if there is no sync root for them)?

Not sure what you're asking here. If you mean SyncBlock instead of Sync-Root, it has nothing to do with thread safety.