WeakReference explanation required - please help!

A

almurph

Hi,

I'm wondering could anyone explain what a weak reference is to me in
C# or perhaps more importantly why it is used.

I ask as I'm reading about an algorithm in the book "C# Cookbook" by
Stephen Teilhet & Jay Hilyard about a light-weight cache. There is a
piece of code that I don't understand, here it is:

*** CODE BEGIN ***

public void AddObj(object key, SomeComplexObj item)
{

Weakreference WR = new WeakReference(item, false);

if(cache.ContainsKey(key))
{
cache[key] = WR;
}
else
{
cache.Add(key, WR);
}
}


*** CODE END ***


I understand most of the algorithm, but i dont understand why the
author is using WeakReference, I dont see the advantage. Can anyone
help me please? Any comments/suggestions/explanations much
appreciated.

Thank you,
Al.
 
P

Peter Bromberg [C# MVP]

A weak reference allows the garbage collector to collect the object while
still allowing the application to access the object. It is valid only during
the indeterminate amount of time until the object is collected -- when no
strong references exist.

When you use a weak reference, the application can still obtain a strong
reference to the object, which prevents it from being collected. However,
there is always the possibility that the garbage collector will get to the
object first before a strong reference is reestablished.

Weak references are generally useful for objects that use a lot of memory,
but can be recreated easily if they are reclaimed by garbage collection.


-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net
 
A

almurph

A weak reference allows the garbage collector to collect the object while
still allowing the application to access the object. It is valid only during
the indeterminate amount of time until the object is collected -- when no
strong references exist.

When you use a weak reference, the application can still obtain a strong
reference to the object, which prevents it from being collected. However,
there is always the possibility that the garbage collector will get to the
object first before a strong reference is reestablished.

Weak references are generally useful for objects that use a lot of memory,
but can be recreated easily if they are reclaimed by garbage collection.

-- Peter
Site:http://www.eggheadcafe.com
UnBlog:http://petesbloggerama.blogspot.com
Short Urls & more:http://ittyurl.net



   I'm wondering could anyone explain what a weak reference is to mein
C# or perhaps more importantly why it is used.
   I ask as I'm reading about an algorithm in the book "C# Cookbook"by
Stephen Teilhet & Jay Hilyard about a light-weight cache. There is a
piece of code that I don't understand, here it is:
*** CODE BEGIN ***
public void AddObj(object key, SomeComplexObj item)
{
   Weakreference WR = new WeakReference(item, false);
   if(cache.ContainsKey(key))
   {
           cache[key] = WR;
   }
   else
   {
           cache.Add(key, WR);
   }
}
*** CODE END ***
   I understand most of the algorithm, but i dont understand why the
author is using WeakReference, I dont see the advantage. Can anyone
help me please? Any comments/suggestions/explanations much
appreciated.
Thank you,
Al.- Hide quoted text -

- Show quoted text -

Thank you
 

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