all groups > dotnet clr > september 2005 >
You're in the

dotnet clr

group:

Garbage Collector Simulator


Garbage Collector Simulator 4eyed
9/24/2005 10:59:44 AM
dotnet clr:
Hello,

I'm trying to figure out a way to tell if there are any remaining
references to an object, sort of the way the garbage collector does.
Is there a way to walk the heap and see if anyone has a reference back
to an object. I want to use this to build some type of object cache.
I would keep a hashtable of objects in a static variable, then expire
the objects when there are no longer any references.

I was thinking thats how the garbage collector knows if it should clean
up objects. I'm not sure where to look.
Re: Garbage Collector Simulator 4eyed
9/24/2005 2:38:59 PM
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.
Re: Garbage Collector Simulator Henning Krause [MVP - Exchange]
9/24/2005 8:25:49 PM
Hello,

for situations like this, there is the WeakReference class. Just encapsulate
your cache objects in a weak reference. Because it is no strong reference,
it does not prevent the GC from collecting the object if it is no longer
referenced. You can check wether the object has been collected with the
WeakReference.IsAlive property.

Greetings,
Henning Krause
MVP - Exchange
http://www.infinitec.de


[quoted text, click to view]

Re: Garbage Collector Simulator Jon Shemitz
9/24/2005 10:34:20 PM
[quoted text, click to view]

why? how is this different from what the garbage collector is already
doing for you?

--

i'm midnightbeach.com and a mtn bike
accident means i'm left handed until mid october,
Re: Garbage Collector Simulator Henning Krause [MVP - Exchange]
9/25/2005 12:00:00 AM
Hello,

[quoted text, click to view]

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


[quoted text, click to view]

Re: Garbage Collector Simulator Shawn B.
9/27/2005 10:18:38 AM
[quoted text, click to view]

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.

[quoted text, click to view]

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

AddThis Social Bookmark Button