Does a value type keep Type pointer + Sync root + Static fields like a reference type?

非 Y 不嫁゛ 提交于 2019-12-21 19:56:52

问题


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.



来源:https://stackoverflow.com/questions/35185528/does-a-value-type-keep-type-pointer-sync-root-static-fields-like-a-reference

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