Can anybody explain the concept of card table and write barriers in Garbage Collection process in .Net? I really can't get the explanation of these terms i.e what are they,how are they useful and how do they paticipate in GC. Any help would be really appreciated.
-
Have you read wilson's card-marking paper? doi 10.1145/66068.66077 – the8472 Sep 04 '16 at 14:42
-
No.If that article contains all of my answers then I would really appreciate if you can share the link. – maverickabhi Sep 04 '16 at 16:42
-
I found this article helpful: https://msdn.microsoft.com/en-us/library/ms973837.aspx – Just another metaprogrammer Oct 28 '16 at 08:28
1 Answers
The card table is an array of bits, one bit for each chunk of 256 bytes of memory in the old generation. The bits are normally zero but when a field of an object in the old generation is written to, the bit corresponding to the objects memory address is set to one. That is called executing the write barrier.
The garbage collector in .NET is generational and has a phase which only traces and collects objects in the young generation. So it goes through the object graph starting with the roots but does not recurse into objects in the old generation. In that way, it only traces a small fraction of the whole object graph.
To find the roots to start tracing from, it scans the programs local and global variables for young generation objects. But it would miss objects only referenced from old generation objects. Therefore it also scans fields of objects in the old generation whose card table bit is set.
Then after the young generation collection is complete it resets all card table bits to zero.

- 19,221
- 20
- 87
- 122
-
The card table has only 1 bit mark per 256 bytes (I think I read 128 bytes somewhere else), and each object can be more/less than 256 bytes, how can this be used to locate exact one object of the old generation? – ROROROOROROR Jun 09 '20 at 03:07
-
1It can't. The 256 bytes of memory referred to by a card table bit can contain zero, one or multiple objects. Therefore you need an auxiliary data structure to keep track of which objects overlap a given card. – Björn Lindqvist Jun 09 '20 at 03:33