I'm not sure that is the behavior i am looking for. Let me give some
more details.
I have a static method GetInstance that either creates a new object or
retrieves an existing one from a hashtable. I need to be able to
refresh this cache periodically. I was thinking of looping through the
hashtable, find out if there are any references to an object, if so,
move it to another hashtable, otherwise destroy it. This way, any
consumer of the object will not get unexpected results and i can clear
out the cache so that new requests will be served with fresh data.
My limted understanding of the weak reference is that it will let the
gc collect the object even if some one has a reference to it.
Hello,
> My limted understanding of the weak reference is that it will let the
> gc collect the object even if some one has a reference to it.
That is not correct. The GC will never collect any objects that are still
referenced by other objects. So if you put your cache object inside a weak
reference, the GC will only collect it, if there are no more other
references to this object.
Greetings,
Henning Krause
MVP - Exchange
http://www.infinitec.de
> I'm not sure that is the behavior i am looking for. Let me give some
> more details.
[quoted text clipped - 9 lines]
> My limted understanding of the weak reference is that it will let the
> gc collect the object even if some one has a reference to it.
> I have a static method GetInstance that either creates a new object or
> retrieves an existing one from a hashtable. I need to be able to
[quoted text clipped - 3 lines]
> consumer of the object will not get unexpected results and i can clear
> out the cache so that new requests will be served with fresh data.
I've done this before. This is called an "Object Pool". In my
implementation, I have a collection of objects. When I need one, I called
GetInstance() (oddly enough, the same thing you call it) and it will find an
available object and return a reference to it, or it will create a new one
(given the maximum number of objects hasn't been created yet). To make this
work, I have another object in between. This object has a status flag
(available, active, unknown), it also has a WeakReference containing the
actual object being referenced.
Now... when an object is returned by GetInstance, the status is changed to
"unavailable". If the consumer fails to call Dispose or Recycle on the
object, then once it goes out of scope, the WeakReference instantly knows as
the Value will be null at this point. Periodically, once ever 15 seconds, a
timer files in the Object Pool. This timer looks for "active" objects whose
WeakReference is null. If that is the case, then the object gets removed
from the pool and a new one will be created if necessary. Otherwise, the
Dispose or Recycle methods on the object (each object in the pool must
adhere to an interface and implement IDisposable) will return the object
back to a default state and set the status to "available".
As you can see, WeakReference is what you're looking for.
> My limted understanding of the weak reference is that it will let the
> gc collect the object even if some one has a reference to it.
No. Not so. The GC will never collect an object that is currently
referenced. The WeakReference basically just allows you to have a
placeholder for an object that may or may not exist. If not, you can put it
there, else you use it. But you have the added benefit that if the
reference it contains gets collected, you'll know. And so, in this case, we
use it to our advantage. It really helps when making an object pool.
Another approach to making object pulls is to use a stack of pooled object.
When you get an instance, you pop it off the stack and return the reference,
and when you are done with it, you push it back on to the stack. Makes
things simple, but that isn't the approach I used, or that you used.
Thanks,
Shawn