How to use WeakReference (dot net 1.1)

R

Rolf Welskes

Hello,
I have the following fundamental problem.
Say I have a class ClsA and a class ClsB

When the system works many objects of ClsB are generated, used and then
freed.
Now I have from any object clsB01 for example to inform all other existing
objects clsB02, clsB03 etc.
For this I have a collection of ClsB objects in ClsA.
Furthermore each ClsB objekt has a back-Referenz to ClsA.
so I have the following:

clsA <-> clsB01
<-> clsB02
<-> clsB03, ....

So for example clsB02 can inform over clsA all other means clsB01, clsB03.

In reality this are many objekts.

Now, this ClsB-Objekts are used anywhere in the program and then they are
never longer used and are free.
If they are not yet used, they must not be informed,
means if I have clsB02, but clsB01 for example is no longer used it must not
be informed.

Since again and again new ClsB objects are generated used and then are again
free,
they would never be garbace collected, because I allways had a reference to
this objects in the Collection of ClsA.

I have thought the following solution:
I do not lay ClsB objects in the collection of ClsA, but WeakReferences to
ClsB.
So if a ClsB object is no longer used any time later the GC would free the
object.
From time to time I check the Collection in ClsA and if a WeakReference
IsAlive is false, I delete it.

So I can inform from any ClsB object another, if it exists and on the other
hand
the Collection would be as small as possible.

Ok.

Now the question.
All this only works, if the fact, that I have a WeakReference on an object
does not influence the garbage collector
when to remove the object the Weakrefence points to.
Means garbage collection of the ClsB objects must run as usally so, as if
there where no WeakReferences on it.
This because if the GC would say there is a weakReference on the object I
clean the object only if there is memory needed,
this could mean, that such an application could have hundrets of megabyte
memory for not used ClsB objects.

this problem is a fundamental design problem and a little complex.
Thank you for any help, realy thank you to any who can help in this case.
Best Regards
Rolf Welskes
 
W

WenYuan Wang [MSFT]

Hi Rolf,

Represents a weak reference, which references an object while still
allowing it to be garbage collected. Weak reference will not influence the
garbage collector.
You can check WeakReference.IsAlive Property for whether object is still
alive, and get it by target property.

Below are some document about weakreference, you may check...
http://msdn.microsoft.com/msdnmag/issues/1100/GCI/
[Garbage Collection: Automatic Memory Management in the Microsoft .NET
Framework]

http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/default.aspx
[Garbage Collection-Part 2: Automatic Memory Management in the Microsoft
.NET Framework]

http://msdn2.microsoft.com/en-us/library/ms404247.aspx
[Using Weak References ]

http://www.codeproject.com/dotnet/garbagecollection.asp?print=true
[Garbage Collection in .NET]

http://www.robherbst.com/blog/2006/08/21/c-weakreference-example/
[C# WeakReference Example]

Additionally, there should be a sample in
<SDK>v1.1\Samples\Technologies\GarbageCollection

Hope this helps, please feel free to let me know if you have anything
unclear.
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

WenYuan Wang [MSFT]

Hi Rolf,

I haven't heard from you two days. Have you resolved the issue so far?
If you met any further issue or you have anything unclear, please don't
hesitate to let me know. I'm glad to work with you.

have a great day,
Sincerely,
Wen Yuan
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top