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 / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

WeakReference explanation required - please help!

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
almurph@altavista.com - 20 Mar 2008 11:33 GMT
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.
Peter Bromberg [C# MVP] - 20 Mar 2008 12:27 GMT
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

> Hi,
>
[quoted text clipped - 31 lines]
> Thank you,
> Al.
almurph@altavista.com - 20 Mar 2008 17:43 GMT
> 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
[quoted text clipped - 52 lines]
>
> - Show quoted text -

Thank you

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.