Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should you collect the young generation first in a full garbage collection?

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.

like image 319
Thomas Padron-McCarthy Avatar asked Oct 20 '08 19:10

Thomas Padron-McCarthy


People also ask

What is young generation garbage collection?

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.

What are the phases of garbage collection?

Garbage collection then goes through the three phases: mark, sweep, and, if required, compaction.

How many generations of garbage collection are there?

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.

What is full garbage collection?

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.


1 Answers

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

like image 198
Chris Bunch Avatar answered Oct 04 '22 08:10

Chris Bunch