Peter Duniho wrote:
Jon Harrop wrote:
There are trade-offs. If the values would have been short-lived then they
could have been recycled without leaving the first generation
But they burden the GC if a collection happens while the objects are
still alive.
The few of these short-lived objects that are alive when the young
generation is collected will burden the GC.
There's no reason to think that the GC is going to always
run a collection just at that instant between the time an inner loop has
finished with all its objects and before it's recreated all of them again.
I was talking about high allocation rates of short lived objects. In that
case, it does not matter when a collection occurs (if indeed a collection
occurs at an instant wrt mutators): there will always be lots of garbage
that can be recycled for future short-lived objects.
but you're
keeping them around for reuse so they'll survive to older generations and
burden the GC more.
They only burden the GC for collections of older generations. Since
most collections will involve only the young generation, long-lived
objects can actually improve the situation, not worsen it.
Potentially, yes. The problem is that only the young generation tends to be
thread local so moving data into the old generation means using a shared
heap which means synchronization and that is so much slower as to offset
the fact that GC cycles of old generations occur less often.
Furthermore, object initialization is typically special
cased and does not incur write barriers whereas mutating any references
in your preallocated objects means additional overhead from the write
barrier as well.
So what? Some times, you win. Some times you don't. That's why you
measure when making optimizations. Many optimizations are heavily
dependent on the exact usage scenario. I'm not saying this technique
always helps, but it's also dumb to say it never helps.
Sure. I'm not saying that it never helps, just that there are non-trivial
trade-offs involved. I'd be surprised if it did help in practice because
the young generation is specifically designed and very heavily optimized
for this exact problem.
"anti-optimization". Testing will tell.