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 / New Users / October 2006

Tip: Looking for answers? Try searching our database.

Garbage Collector

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
virtualwarp - 22 Oct 2006 18:09 GMT
Hey everyone,

i'm currently trying to implement a WeakDictionary, where all keys are
WeakReferences. Now I want to remove the keys when the targets are no longer
reachable. Is there any possibility to get informed when an object is garbage
collected?

Greetings

Heiko Böttger
Mattias Sjögren - 22 Oct 2006 21:52 GMT
>Is there any possibility to get informed when an object is garbage collected?

No, there's no such notification in managed code.

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

virtualwarp - 23 Oct 2006 07:52 GMT
Thank you for your answer.

Thats want I thought.

> >Is there any possibility to get informed when an object is garbage collected?
>
> No, there's no such notification in managed code.
>
> Mattias
Morten Wennevik - 23 Oct 2006 08:23 GMT
> Thank you for your answer.
>
[quoted text clipped - 11 lines]
>> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
>> Please reply only to the newsgroup.

I'm not sure if this is applicable, but if you control the objects being  
garbage collected, you could add a finalizer who would notify some method  
that it is about to get garbage collected.  The garbage collector calls  
the finalizer before collecting it unless the objects finalizer is  
suppressed (typically in the Dispose method).

Signature

Happy Coding!
Morten Wennevik [C# MVP]

Tigger - 23 Oct 2006 08:45 GMT
My solution was to check if the weak reference had lost its target to
the GC before returning it.

I also implemented a timed process which purged items that had been
GCed.

Tigger

> > Thank you for your answer.
> >
[quoted text clipped - 17 lines]
> the finalizer before collecting it unless the objects finalizer is
> suppressed (typically in the Dispose method).
virtualwarp - 26 Oct 2006 21:49 GMT
Thank you for your answer.

I have already seen some examples on the net, they all make use of timers.
The problem with these solutions is that there is no collection containing
the GCed objects and I have to iterate through the whole list of
weakreferences. When you are dealing with long lists this isn't very fast. I
would prefere to setup something like a GC listener. In Java there is a
ReferenceQueue which is bound to several references. Whenever a weak
reference is set to null, ot is added to the queue. Since the queue contains
only the references pointing which objects where GCed. You can simple removed
them from your key list.

> My solution was to check if the weak reference had lost its target to
> the GC before returning it.
[quoted text clipped - 25 lines]
> > the finalizer before collecting it unless the objects finalizer is
> > suppressed (typically in the Dispose method).
virtualwarp - 26 Oct 2006 21:54 GMT
I already thought about this solution, but as you mentioned the object have
to be under my control and wouldn't rely on this.

> > Thank you for your answer.
> >
[quoted text clipped - 17 lines]
> the finalizer before collecting it unless the objects finalizer is  
> suppressed (typically in the Dispose method).
Brian Gideon - 26 Oct 2006 22:19 GMT
Heiko,

This is an excellent question.  The only I way I know of would be to
add code to the finalizer, but like you said, if the object isn't under
your control then that will be impossible.  What I did was everytime
something was added to the dictionary I called a Purge method that
would loop through items to find which ones were unreachable.  In
hindsight that was probably a bad idea because inserts into my
dictionary now became O(n) instead of O(1), but I wasn't storing a lot
to begin with so it wasn't that big of an issue.  Honestly, some
combination of purging on inserts and using a timer might be the best
you can do.  Sad isn't it?

One tip...remember to write your own IEnumerator for your collection
and make sure you filter out unreachable objects.

Brian

> Hey everyone,
>
[quoted text clipped - 6 lines]
>
> Heiko Böttger

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.