What is Object Pooling in Java?

Himanshu picture Himanshu · Feb 7, 2011 · Viewed 46.1k times · Source

What is object pooling and what is a weak object reference ?

How can we implement them using Java?

Answer

duffymo picture duffymo · Feb 7, 2011

An object pool is a collection of a particular object that an application will create and keep on hand for those situations where creating each instance is expensive. A good example would be a database connection or a worker thread. The pool checks instances in and out for users like books out of a library.

Usually object pooling is handled by a Java EE application server. If you need to do it yourself, best to use something like Apache's object pool. Don't write one yourself; thread safety and other issues can make it complicated.

Here's a good reference on weak object references.