• Post Reply Bookmark Topic Watch Topic
  • New Topic

General object pooling  RSS feed

 
Sebastian Oerding
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've a question about object pooling.

I've to implement an algorithm for object pooling. However when I
investigated a bit, this becomes more complex as visible at a first
glance.

Just using Google gives tons of results for object pooling in Java.
In the cases I saw, some advanced data structure like Hashtables,
HashMaps or Sets are used as object pool. But whenever an instance is
requested from the pool, an iterator is called to get the next
(unblocked) instance. Exploring the source code for the mentioned
classes I realized that with this approach, every time when an iterator
is called, a new instance of an appropriate iterator is instantiated.

When using object pooling we want to avoid the expensive instantiation
of new objects, but with this approach, we only exchange the
instantiation of our objects against the instantion of an iterator.

This leads to the first questions:
1. How expensive is the instantiation of an iterator?
2. Does this approach of object pooling only makes sense, when the
instantiation of our objects is really expensive compared to the
instantiation of the iterators or have I overseen something?

An approach to avoid this, would be to use arrays.
Lets say, we have our instances in A[]. Whenever an instance is required
we choose the first available instance from A[] (entry at index i with
the entry not being null), set A[i] to null and return the instance.
After having our task finished we set A[i] back to refer to the instance
of A used for the task.

Besides the limitations this would add to the pooling (when adding a new
instance to the pool the array has to be fully recreated as the size is
immutable), when searching for an instance, we have to run through the
array with "for (int index = 0; index < A.length; i++) { if (A[i] !=
null) ...}"
How expensive is running through the array this way compared to instance
creation of an iterator? Is this approach besides its disadvantages
faster than the common one?

With kind regards zyrano
 
R van Vliet
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer your questions :

1) Not expensive at all. Lightweight object allocation and deallocation happens at very, very low cost in modern VMs.
2) Yes, as mentioned it is completely useless to use any kind of object pooling for lightweight objects. Objects that are expensive to create (because they access remote resources for example or initiate connections of some sort) are candidates for pooling but that's really about it.
3) Iterating over an array is faster than using in iterator in all cases (although in the case of an iterator backed by ArrayList for example the performance would be similar). That said your array approach would have to be aware of thread concurrency which introduces locking and synchronization to your solution which doesn't help performance at all.

All that said, you're talking about performance differences that can probably only be measured in microseconds so it sounds like premature optimization to me. Prioritize clean, maintainable code. Iterators in most cases are easier to read in code than array iteration code surrounded by locking.
 
Mike Peters
Ranch Hand
Posts: 67
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastian Oerding wrote:When using object pooling we want to avoid the expensive instantiation
of new objects, but with this approach, we only exchange the
instantiation of our objects against the instantion of an iterator.

This leads to the first questions:
1. How expensive is the instantiation of an iterator?
2. Does this approach of object pooling only makes sense, when the
instantiation of our objects is really expensive compared to the
instantiation of the iterators or have I overseen something?


As far as I know a HashMap does not instantiate an iterator when you use the get method. Often object pooling is not used to improve performance, but to prevent memory or resource depletion. Maybe this helps.
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for reference, you may want to take a look at the Apache Commons Pool source code. It's a small library, so it shouldn't be too difficult to understand.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!