Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Object Pooling in Java?

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

How can we implement them using Java?

like image 243
Himanshu Avatar asked Feb 07 '11 13:02

Himanshu


People also ask

Why do we need object pooling?

Object Pooling is a great way to optimize your projects and lower the burden that is placed on the CPU when having to rapidly create and destroy GameObjects.

What is object pool pattern in Java?

The object pool pattern is a software creational design pattern that uses a set of initialized objects kept ready to use – a "pool" – rather than allocating and destroying them on demand. A client of the pool will request an object from the pool and perform operations on the returned object.

What is object pooling and recycling patterns?

Object Pool is a design pattern, that works with a set of pre-initalized entities, instead of disposing and then re-creating them on demand. Whenever the client of the pool is done working with an object, it just returns it to the pool for recycling.

What is a pool in coding?

In computer science, a pool is a collection of resources that are kept, in memory, ready to use, rather than the memory acquired on use and the memory released afterwards.


2 Answers

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.

like image 164
duffymo Avatar answered Sep 19 '22 18:09

duffymo


Check common-pools

provides an Object-pooling API

It is generally used for objects whose creation is expensive. In order to avoid that you maintain a pool of N pre-created objects and reuse them.

like image 32
Bozho Avatar answered Sep 19 '22 18:09

Bozho