Re: Does object pooling *ever* make sense?
Chris <spam_me_...@goaway.com> wrote:
I've read recently that object allocation in recent JVMs is so fast that
it doesn't make sense to create object pools.
Andy Dingley wrote:
Most of my pooled objects are some sort of reference to an external
resource (e.g. a DB connection) that's inherently expensive. I don't
care what the cost of the object itself is, they're pooled to
economise on this external cost and that's not changed by any JVM
improvement.
Given the low cost of Java object creation I find it hard to justify
pooling for objects that are simply entirely Java anyway. What might
cause me to want to pool them? A stateless singleton is justifiable
but anything with state attached to it probably means as much effort
to create the distinct state for each pooled use of the object as it
does to create a whole new object.
And remember Gordon's points:
Gordon Beaton wrote:
A disadvantage to pooling is that it increases the average age of your
objects, resulting in more live objects at any given time, more
objects that live beyond the "nursery", and consequently more work for
the garbage collector.
You also have to manage the object pool.
That "more work for the garbage collector" relates to the fact that it's
harder to garbage collect out of the tenured generation than the nursery.
Except for resource gates, it rarely helps to pool.
- Lew