5

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)?
Community
  • 1
  • 1
Anton Lyhin
  • 1,925
  • 2
  • 28
  • 34
  • Related to static methods: http://stackoverflow.com/questions/2413483/virtual-method-tables – Anton Lyhin Feb 03 '16 at 21:01
  • How is ValueType.GetType() able to determine the type of the struct?http://stackoverflow.com/questions/926352/how-is-valuetype-gettype-able-to-determine-the-type-of-the-struct – Anton Lyhin Feb 05 '16 at 22:49

1 Answers1

2

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.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • Are value type static methods stored in "High Frequency Heap" as well? Will a type object contain the struct's static method? This question is avioded for some reason in CLR books. Thank you. – Anton Lyhin Feb 03 '16 at 19:41
  • 1
    @Spirit Yes. To quote Jon Skeet [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.](http://www.developerfusion.com/article/4705/memory-in-net-what-goes-where/2/) – Sriram Sakthivel Feb 03 '16 at 19:48
  • For second question, Type object doesn't contain a method. It just contains the metadata. What you mean by where methods are stored? It's jitted and executed using the address ? – Sriram Sakthivel Feb 03 '16 at 19:49
  • Here is the answer for value type static methods: "When a type is loaded into the CLR, a method table is created and initialized for the type. This method table contains one entry for every new method introduced by the type as well as entries for any virtual methods inherited by the type". I hope that "a type" means either reference or value type. – Anton Lyhin Feb 03 '16 at 20:35
  • But it is still not clear how thread stack piece of data gets aknowledged of its type, static methods and fields? For reference types we have a type-object pointer. – Anton Lyhin Feb 03 '16 at 21:03
  • 1
    @Spirit When a struct is located in stack, it is just raw bytes. No Type is associated with it. Type information will be associated with it when the strut is boxed using `box` opcode. – Sriram Sakthivel Feb 04 '16 at 06:40
  • Box value to specific Type (the type which can be detected for value types at compile time because value types are never virtual by definition of value type). – Anton Lyhin Feb 05 '16 at 22:47