> I'm going to use option b). The way I have it implemented is that the
> object remains in the pool, or, more precisely, the ObjectPool manager
> still
> has knowledge of the object via WeakReference so that if the Target ==
> null
> then the ObjectPool manager will reclaim it back into the pool.
This is too late, Target == null means that the object has been collected by
a GC and no longer exists. You cannot therefore reclaim it, unless you mean
you'll recreate a copy in that instance? However in this scenario, Target
might not become null for quite some time. If your pool only creates a
certain number of a particular object, this could lead to resource
starvation.
> In reality,
> I don't wan that to happen; instead I want the consumer to call
> IPoolableObject.Recycle() which will in turn put the object back into the
> pool.
Doesn't that mean that the object has to know how to put itself back in the
pool? Wouldn't it be better for encapsulation to have an
ObjectPool.Reclaim(object) method instead?
> I'm not certain right now one way or the other whether it is good or bad
> design to have the pool still try to track it through the WeakReference as
> a
> last ditch effort to keep resources under control (it checks by default
> once
> every 5 seconds but can be changed).
Rather than try to track every copy out in the wild, how about tracking
every object in you pool. Let's say you want 5 copies of ObjectA, your pool
creates them. 3 of them get handed out to other procesees. A few seconds
later, 1 gets returned. Now your "pool management" function kicks in. It
analyzes the pool and determines that it is 2 short on ObjectA, so it
creates 2 and adds them to the pool. Now one of the "old" ObjectA's is
returned, the pool looks and sees that it already has 5, and therefore
destroys the incoming one. It then gives out one of it's copies, and then
another "old" object A returns, which it places back in it's pool.
> It works really well except in ASP.NET.
>
> I am implementing a ThreadPool in the ObjectPool and each IPoolableObject
> will operate in its own thread (thats what I'm aiming for, as you point
> out
> in B.).
I'm really unsure of what you mean here. Is the ObjectPool a service that
polls sockets? Is the ObjectPool in a different AppDomain? Surely, the
ObjectPool and IPoolableObjects will operate in the Thread that is using
them rather than their own thread?
> Perhaps when I get the threading worked out I can upload it somewhere and
> you can look at it if you want.
Sure.
> BTW: This is all in 2005 using generics. I don't have the object pool
> containing multiple objects yet, instead, you instance an
> ObjectPool<ObjectType> but in the end I want one pool to be able to
> support
> multiple object types.
class ObjectPool {
...
public T GetObject<T>() {
...
}
public void ReturnObject<T>(T @object) {
...
}
}
> Thank you for your help.
No problem.
> Thanks,
> Shawn
Shawn B. - 21 Apr 2005 18:15 GMT
> > I'm going to use option b). The way I have it implemented is that the
> > object remains in the pool, or, more precisely, the ObjectPool manager
[quoted text clipped - 9 lines]
> certain number of a particular object, this could lead to resource
> starvation.
Actually, my object isn't in the pool, an internal object that hold a
reference to an instance of my object is in the pool, along with state
information (active, available, unknown). When WeakReference.Target ==
null, then this container object creates another instance and marks it as
available (if the pool wants to have n number of object in the pool at all
times, by default, the pool grows and shrinks according to demand). If in
onDemand mode, then when the Target == null, then the container object gets
removed from the pool.
> > In reality,
> > I don't wan that to happen; instead I want the consumer to call
[quoted text clipped - 4 lines]
> pool? Wouldn't it be better for encapsulation to have an
> ObjectPool.Reclaim(object) method instead?
Ah... yes. Details details. When the pooled object gets created, a
reference to the pool that created it gets passed in (in the constructor ::
potential circular reference? potentially)... so when you call
IPoolableObject.Recycle it actually calls ObjectPool.Recycle(this) for you.
> > I'm not certain right now one way or the other whether it is good or bad
> > design to have the pool still try to track it through the WeakReference as
[quoted text clipped - 24 lines]
> ObjectPool and IPoolableObjects will operate in the Thread that is using
> them rather than their own thread?
No, object pool does not poll sockets. It is actually just another type of
a collection (it derives from System.Object) and initially, I intended it to
be in the same AppDomain and it currently is. The reason I say that is
because its primary use will be in an ASP.NET web application. I want each
object (up to a certain limit) to be in its own thread or I'l have all the
web pages contending for an object pool the lives in the Application
variable (not sure where else to put it besides in a Service and marshal
it). The second place it will be used will be in our backend service
servers, since we have many services that do many different things, but this
object pool doesn't solve every problem and won't be the right thing to use
in all services. Just where it makes sense.
> > Perhaps when I get the threading worked out I can upload it somewhere and
> > you can look at it if you want.
[quoted text clipped - 24 lines]
> > Thanks,
> > Shawn
Thanks,
Shawn
Sean Hederman - 21 Apr 2005 23:09 GMT
> Ah... yes. Details details. When the pooled object gets created, a
> reference to the pool that created it gets passed in (in the constructor
> ::
> potential circular reference? potentially)
Not a problem in .NET thank goodness ;D