Re: Does object pooling *ever* make sense?
Joe Seigh wrote:
Not so much that the gc is so much faster than it's not really all that
worse
and the effects of tying up a lot of memory in object pools is more
problematic.
The Java mechanisms and GC tend to be much faster than hand-rolled object
pooling (except for resource guards, but then it's not memory that is the slow
part any more).
According to what I've read.
The copying collector relies on having enough memory so you don't run out
between normal GC cycles.
What do you mean by "between normal GC cycles"? From what I've read, GC runs
when memory runs low, not on some kind of timer.
If you are running out of memory and forcing GC to run more often,
you could just use an object pool and use a WeakReference to
point to the object pool. The pool will be reclaimed on every GC cycle
more or less. The only time a pool will exist is when it's active. I'm
assuming weak references don't slow down GC all that much.
I have not run across any reference that ties weak references to any
performance considerations for garbage collection.
The point is that you do not need to use object pooling. The Java memory
allocator is more efficient, by all accounts, than any local policy can hope
to achieve, and using custom schemes is only likely to interfere with the Java
mechanism.
The whole point of the Java memory mechanism is to remove responsibility for
memory management from the programmer. Why would anyone want to mess with that?
If you want to tune the collector, that's an entirely different matter. You
are much more likely to gain joy from the java -X and -XX parameters than any
in-process approach.
There is a common theme running in this newsgroup of people not wanting Java
to be Java. This seems to blind them to the strengths of the language, as they
focus on perceived weaknesses. No language is perfect. Part of the art of
programming involves playing to the strengths of the tools at hand.
Java provides a lot of help with memory management. Use what it offers before
trying to defeat it.
- Lew