Structuring Metadata for Collection

We have already touched on object layout in other chapters, but we should now look at it again in more detail. All object instances begin with a pointer to their method table; as we have seen, the space allocated for this pointer is overloaded during garbage collection to contain two critical bit flags, one for marking the object as live and the other for marking the object as pinned. It is guaranteed that the normal activity of the execution engine will be suspended during a collection, leaving the collector free to monkey around with memory. Because of this, the garbage collector can get away with overlaying bit flags directly; the pointer itself will always contain zeros in the necessary locations because of the way that memory is laid out, and the execution engine will not try to redirect through the pointer during collection.

The MethodTable, as we saw in Chapter 5, contains more than just a table of method pointers. It is also a useful place to store additional information related to garbage collection that is per-type rather than per-instance. As an example of this kind of per-type information, a set of flag bits for MethodTable can be seen in Example 7-15.

Example 7-15. MethodTable flags include garbage collection information (defined in clr/src/vm/class.h)

 enum { enum_flag_Array = 0x10000, enum_flag_large_Object = 0x20000, enum_flag_ContainsPointers = 0x40000, enum_flag_ClassInited = 0x80000, enum_flag_HasFinalizer = 0x100000, enum_flag_Sparse ...

Get Shared Source CLI Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.