467,888 Members | 1,632 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,888 developers. It's quick & easy.

WeakReference explanation required - please help!

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.
Mar 20 '08 #1
  • viewed: 1303
Share:
2 Replies
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
"al*****@altavista.com" wrote:
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.
Mar 20 '08 #2
On Mar 20, 11:27*am, Peter Bromberg [C# MVP]
<pbromb...@yahoo.NoSpamMaam.comwrote:
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

"almu...@altavista.com" wrote:
Hi,
* *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
Mar 20 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Buck Rogers | last post: by
70 posts views Thread by rahul8143 | last post: by
6 posts views Thread by Andrew Hayes | last post: by
2 posts views Thread by Dave Taylor | last post: by
14 posts views Thread by Akhil | last post: by
12 posts views Thread by Pieter | last post: by
9 posts views Thread by tsushio | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.