views:

188

answers:

1

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.

+4  A: 

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).

Chris Bunch