Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / September 2005

Tip: Looking for answers? Try searching our database.

Garbage Collector Simulator

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
4eyed - 24 Sep 2005 18:59 GMT
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.
Henning Krause [MVP - Exchange] - 24 Sep 2005 19:25 GMT
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

> Hello,
>
[quoted text clipped - 7 lines]
> I was thinking thats how the garbage collector knows if it should clean
> up objects.  I'm not sure where to look.
4eyed - 24 Sep 2005 22:38 GMT
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.
Henning Krause [MVP - Exchange] - 25 Sep 2005 11:27 GMT
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.
Shawn B. - 27 Sep 2005 18:18 GMT
> 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
Jon Shemitz - 25 Sep 2005 06:34 GMT
> 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.

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

Signature

i'm midnightbeach.com and a mtn bike
accident means i'm left handed until mid october,
so please pardon lower case and terseness.


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.