Eske Sparsø
Eske Sparsø

Reputation: 133

Details of write barriers in the .Net Garbage Collector

I have a large T[] in generation 2 on the Large Object Heap. T is a reference type. I make the following assignment:

T[0] = new T(..);

I've read through many questions and articles, including the ones below, but I still don't think I've found a clear answer. I'm aware that an entire range of memory is marked as dirty, not individual objects, but is the new array entry or the array itself the basis of this?

card table and write barriers in .net GC

Garbage Collector Basics and Performance Hints

Upvotes: 2

Views: 761

Answers (2)

vgru
vgru

Reputation: 51214

Which object(s) are marked as dirty for the next Gen0/Gen1 mark phases of GC? The entire array instance, or just the new instance of T?

128B block containing the start of the array will be marked as dirty. The newly created instance (new T()) will be a new object, so it will first be checked through a Gen 0 collection without the card table.

For simplicity, presuming that the start of the array is aligned on a 128B boundary, this means the first 128B will be invalidated, so presuming T is a reference type and you're on a 64-bit system, that's first 16 items to check during the next collection.

Will the next Gen0/Gen1 GC mark phase have to go through every item of the array? (That would seem unnecessary and very inefficient.)

Just these 16 to 32 items, depending on the pointer size in this architecture.

Are arrays special in this regard? Would it change the answer if the collection were e.g. a SortedList and I added a new, maximal item?

Arrays are not special. A SortedList<K,T> maintains two arrays internally, so more blocks will end up dirty in the average case.

Upvotes: 1

TakeMeAsAGuest
TakeMeAsAGuest

Reputation: 995

pretty sure its tracking array slots, not the root which is holding reference to array object itself. btw if particual card is set dirty, it has to scan 4k of memory. ive read somewhere its now using windows' own mechanism which lets you get notifications if memory range in interest is written to.

Upvotes: 0

Related Questions