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

Re: Garbage Collector by Mattias

Mattias
Sun Oct 22 15:52:28 CDT 2006

>Is there any possibility to get informed when an object is garbage collected?

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


Mattias

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

Re: Garbage Collector by virtualwarp

virtualwarp
Mon Oct 23 01:52:01 CDT 2006

Thank you for your answer.

Thats want I thought.

"Mattias Sjögren" wrote:

> >Is there any possibility to get informed when an object is garbage collected?
>
> No, there's no such notification in managed code.
>
>
> Mattias
>
> --
> Mattias Sjögren [C# MVP] mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.
>

Re: Garbage Collector by Morten

Morten
Mon Oct 23 02:23:48 CDT 2006

On Mon, 23 Oct 2006 08:52:01 +0200, virtualwarp
<virtualwarp@discussions.microsoft.com> wrote:

> Thank you for your answer.
>
> Thats want I thought.
>
> "Mattias Sjögren" wrote:
>
>> >Is there any possibility to get informed when an object is garbage
>> collected?
>>
>> No, there's no such notification in managed code.
>>
>>
>> Mattias
>>
>> --
>> Mattias Sjögren [C# MVP] mattias @ mvps.org
>> 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).

--
Happy Coding!
Morten Wennevik [C# MVP]

Re: Garbage Collector by Tigger

Tigger
Mon Oct 23 02:45:06 CDT 2006

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

Morten Wennevik wrote:
> On Mon, 23 Oct 2006 08:52:01 +0200, virtualwarp
> <virtualwarp@discussions.microsoft.com> wrote:
>
> > Thank you for your answer.
> >
> > Thats want I thought.
> >
> > "Mattias Sj=F6gren" wrote:
> >
> >> >Is there any possibility to get informed when an object is garbage
> >> collected?
> >>
> >> No, there's no such notification in managed code.
> >>
> >>
> >> Mattias
> >>
> >> --
> >> Mattias Sj=F6gren [C# MVP] mattias @ mvps.org
> >> 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).
>=20
> --=20
> Happy Coding!
> Morten Wennevik [C# MVP]


Re: Garbage Collector by virtualwarp

virtualwarp
Thu Oct 26 15:49:02 CDT 2006

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.



"Tigger" wrote:

> 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
>
> Morten Wennevik wrote:
> > On Mon, 23 Oct 2006 08:52:01 +0200, virtualwarp
> > <virtualwarp@discussions.microsoft.com> wrote:
> >
> > > Thank you for your answer.
> > >
> > > Thats want I thought.
> > >
> > > "Mattias Sjögren" wrote:
> > >
> > >> >Is there any possibility to get informed when an object is garbage
> > >> collected?
> > >>
> > >> No, there's no such notification in managed code.
> > >>
> > >>
> > >> Mattias
> > >>
> > >> --
> > >> Mattias Sjögren [C# MVP] mattias @ mvps.org
> > >> 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).
> >
> > --
> > Happy Coding!
> > Morten Wennevik [C# MVP]
>
>

Re: Garbage Collector by virtualwarp

virtualwarp
Thu Oct 26 15:54:02 CDT 2006

I already thought about this solution, but as you mentioned the object have
to be under my control and wouldn't rely on this.

"Morten Wennevik" wrote:

> On Mon, 23 Oct 2006 08:52:01 +0200, virtualwarp
> <virtualwarp@discussions.microsoft.com> wrote:
>
> > Thank you for your answer.
> >
> > Thats want I thought.
> >
> > "Mattias Sjögren" wrote:
> >
> >> >Is there any possibility to get informed when an object is garbage
> >> collected?
> >>
> >> No, there's no such notification in managed code.
> >>
> >>
> >> Mattias
> >>
> >> --
> >> Mattias Sjögren [C# MVP] mattias @ mvps.org
> >> 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).
>
> --
> Happy Coding!
> Morten Wennevik [C# MVP]
>

Re: Garbage Collector by Brian

Brian
Thu Oct 26 16:19:19 CDT 2006

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

virtualwarp wrote:
> 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 lon=
ger
> reachable. Is there any possibility to get informed when an object is gar=
bage
> collected?=20
>=20
> Greetings
>=20
> Heiko B=F6ttger