I'm writing a program that contains a generational garbage collector. There are just two generations. What I wonder is: When doing a full collection, do I gain anything (performance-wise) by first collecting the younger objects, promoting the survivors to the old generation, and then collecting the old generation, or should I just garbage collect everything in one sweep? I'm not sure which way people usually do it.
I'm using the two-step method now, since it was a bit simpler to implement, but perhaps a one-step method would be more efficient?
The garbage collector is non-copying, if that matters.
The Young Generation is where all new objects are allocated and aged. When the young generation fills up, this causes a minor garbage collection. Minor collections can be optimized assuming a high object mortality rate. A young generation full of dead objects is collected very quickly.
Garbage collection then goes through the three phases: mark, sweep, and, if required, compaction.
The C# garbage collection uses three generations in total: Generation 0—This generation holds short-lived objects. Here's where the collection process happens most often.
Major or Full Garbage Collection: It is said to have occurred when the objects that survived the minor garbage collection are copied into the old generation or permanent generation heap memory are removed.
It depends on how often you promote survivors. If you promote them often, then it looks like you'll do a lot better by doing GC in one sweep. If you don't, then it looks like they'll be pretty similar.
Either way, it seems as if you do a little bit of redundant work by doing it in two phases. For example, anyone that gets promoted gets inherently checked twice (once as young and again as old). Again, if this doesn't happen too often, I'd stick with the simpler two-step method (since you already have it working and there's little to gain).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With