*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Object Pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Object Pool" Watch "Object Pool" New topic
Author

Object Pool

Palash Sahu
Greenhorn

Joined: Dec 06, 2006
Posts: 13
Can any body help me regarding object pooling.
I want to create a pool of message processor so that i need not create a new
new instance of processor to process every message.Bcoz in that lot of instance will be created and memory issue will be big concern.
Chris Nappin
Ranch Hand

Joined: Aug 04, 2005
Posts: 36
Can you elaborate on your requirements?

Do you have to consider threading? Does the pool need to be dynamic?

If threading is not an issue then the best way to minimise memory use is to use the singleton pattern. However whatever solution you decide to use can be hidden behind a factory method so you can change it in the future if needed.


Technical Architect, SCJP, SCWCD
Palash Sahu
Greenhorn

Joined: Dec 06, 2006
Posts: 13
Originally posted by Chris Nappin:
Can you elaborate on your requirements?

Do you have to consider threading? Does the pool need to be dynamic?

If threading is not an issue then the best way to minimise memory use is to use the singleton pattern. However whatever solution you decide to use can be hidden behind a factory method so you can change it in the future if needed.


Thanks a ton for your reply.
Actualy scenario is number of message processors will be invoked by Message Driven POJO(MDP), a spring's solution.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I don't know Spring ... does each inbound message create a new MDP? If not, that is if Spring is pooling MDPs, you can just have one message processor per MDP as a member variable and piggy back on that pool.

You could easily occupy more memory with a pool of something than you would by creating and destroying them. The cost of creation is a key factor in deciding to make a pool. If new MessageProcessor() doesn't do much but initialize a couple member variables, I probably wouldn't bother with the pool.

BTW: The suggestion to hide the decision behind a factory which might use a pool or a singleton or a simple creator is cool. Make decisions as late and low as you can, isolate sites of change, all that goodness.
[ December 07, 2006: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Chris Nappin
Ranch Hand

Joined: Aug 04, 2005
Posts: 36
Originally posted by Palash Sahu:


Thanks a ton for your reply.
Actualy scenario is number of message processors will be invoked by Message Driven POJO(MDP), a spring's solution.


I'm familiar with Spring but not with it's Messaging integration, however you can certainly define whether Spring beans are singletons or not via the "singleton" bean attribute in the application context XML file.

Obviously a singleton solution will need to be a stateless class to avoid threading issues.

You can also get Spring to use static or instance factory methods (instead of constructors) so if you do have to go with your own pool management then your Factory method could hide that fact from the Spring dispatcher?
Palash Sahu
Greenhorn

Joined: Dec 06, 2006
Posts: 13
Thanks again all of you guys.
I am encounter problem after this message reception..i.e message processing
But as I am framework developer,not a application developer.I can't do
the actual implementation of message processing.It will be done by application
developer.So I need to build sub class object of message processor
depending data provided with message by application developer.
A point to be noted, some data(i.e msgprocessorClassName) is to be arrived with
the message which is needed to make message processor.
Here is the right challenge.How this data (i.e msgprocessorClassName)
is to be passed to getMessageProcessor() Method.
How can I design message processing infrustructure.
Any help will be highly apprecited.
akilan irudaya raja
Greenhorn

Joined: Feb 19, 2004
Posts: 26
Your are not going to implement message processor. But you need to
design the intelligence which selects the appropriate MessageProcessor, Is it so?


Java Programmar SCJP,SCWCD,SCBCD.
Palash Sahu
Greenhorn

Joined: Dec 06, 2006
Posts: 13
Originally posted by akilan irudaya raja:
Your are not going to implement message processor. But you need to
design the intelligence which selects the appropriate MessageProcessor, Is it so?


You pick the right point discussing over here.
For the appropriate MessageProcessor some input (such as msgProcessorClassName) is required with message from the application developed
by appliocation developer.But the challenge is this data(msgProcessorClassName)can be passed to getMessageProcessor() Method in a suitable way.
I have a AbstractMessageProcessor class which has some methods like
process,reprocess,postprocess etc.
Application developer will override these methods in the actual message
processing impplementation.My job is to provide a facade to build a sub class object of AbstractMessageProcessor (i.e actual message
processing impplementation) through some input(msgProcessorClassName)
with message from a application developed by apllication developer.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Palash Sahu:
Can any body help me regarding object pooling.
I want to create a pool of message processor so that i need not create a new
new instance of processor to process every message.Bcoz in that lot of instance will be created and memory issue will be big concern.


What exactly do you mean by "a lot"? How big are they? And how long will they live?

Java is very good at managing a lot of small, short lived objects. I would worry that the use of a object pool could be premature, unnecessarily complicating the design. Could you reproduce a memory issue in tests?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Palash Sahu
Greenhorn

Joined: Dec 06, 2006
Posts: 13
Originally posted by Ilja Preuss:


What exactly do you mean by "a lot"? How big are they? And how long will they live?

Java is very good at managing a lot of small, short lived objects. I would worry that the use of a object pool could be premature, unnecessarily complicating the design. Could you reproduce a memory issue in tests?

A approprite message processor is related to a particular message.
So for every message I have to create new instance of message processor.
That's why I was thinking of Object Pool concept.
I think maximum Pool size of 10 or 15 will suffice my requirement.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Simple answer for pools: You can make a pool very quickly with a BlockingQueue. Put some number of objects into the queue at startup. Each process gets one out, uses it, and puts it back. If there are more threads than objects some of the threads will block on the get until another thread puts one back in.

Longer answer: Use some kind of profiling to prove you need one first.

If you have enough objects for all the threads, the memory use will be constant N instead of going up and down with the number of threads. No memory savings at all.

If you don't have enough objects for all the threads, there is some memory savings but you slow things down on the blocking gets.

Regardless of those factors, a pool will reduce object creation and garbage collection. If the creation takes some time or if GC fragments memory in a bad way a pool can pay off big. If creation and GC are not problems, the pool is just overhead and complexity. The fastest and most defect-free components are the ones that aren't there.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object Pool