Is object pooling still necessary?

I’ve have implemented a transport server in Java, which creates many objects of the same type. In consideration of the compiler improvements does it make sense to use object pools like http://jakarta.apache.org/commons/pool/ nowadays?What do you thing?Thx,Chris
[295 byte] By [Chris80a] at [2007-10-2 4:05:03]
# 1
With JSE 5.0, it isn't normally a good idea unless the object holds some expensive to produce or allocate resource. Consider http://www-128.ibm.com/developerworks/java/library/j-jtp09275.html?ca=dgr-jw22JavaUrbanLegends
cmccorveya at 2007-7-15 23:27:48 > top of Java-index,Java Essentials,Java Programming...
# 2

No.

Or only very rarely. Only if you have an object that is particularly costly to create, e.g. a state machine for a parsing or cryptographic engine.

With Java your PC can create and garbage collect a couple of hundred million objects per second. It's slower to pool them than to create new ones.

sjasjaa at 2007-7-15 23:27:48 > top of Java-index,Java Essentials,Java Programming...
# 3
cmccorvey and sjasja,thx a lot for your statements!Chris
Chris80a at 2007-7-15 23:27:48 > top of Java-index,Java Essentials,Java Programming...
# 4

> Or only very rarely. Only if you have an object that is particularly

> costly to create, e.g. a state machine for a parsing or

> cryptographic engine

It is quite interesting though. I built a parser recently, and pooled the state-machines. However, I did all allocation for buffers (byte[]) on demand.

Performance wasn't great - so I created an allocator interface - and put my HeapAllocator head-to-head with a PooledAllocator.

Bang: 35% performance improvement with pooling.

aconst_nulla at 2007-7-15 23:27:48 > top of Java-index,Java Essentials,Java Programming...
# 5

One situation where I have used pooled objects is buffers in a web server. I would construct a web page into a byte[], write it, and return the byte[] into a pool.

100 hits/s at 4 kB/page = 400 kB of garbage per second. Which would have led to pretty frequent garbage collection if I hadn't pooled.

I wonder how well your average JSP engine handles that...

sjasjaa at 2007-7-15 23:27:48 > top of Java-index,Java Essentials,Java Programming...
# 6

> I wonder how well your average JSP engine handles that...

Pretty darn well, as it happens :o)

Tomcat does hardly any allocation per request.

Nearly all of the internal tomcat classes can be "disposed" or "recycled".

It increases the code complexity quite a bit (you dont want to be leaking those objects!!) - but as I found out myself - is ultimately worth it.

Its also very useful for helping in a centralised dos attack handling strategy.

aconst_nulla at 2007-7-15 23:27:48 > top of Java-index,Java Essentials,Java Programming...
# 7
> Pretty darn well, as it happens :o)Unless of course you meant the application (e.g servlet / jsp) creating the buffers - in which case its out of the hands of the container i'd say :o)
aconst_nulla at 2007-7-15 23:27:48 > top of Java-index,Java Essentials,Java Programming...