Generational behavior for the garbage collector

Same as #5227 but living in this repo & squashed.