« Controlling Windows Firewall using C# via COM Interop | Main | Dll heaven: Executing multiple versions of the same assembly »

January 15, 2008


TrackBack URL for this entry:

Listed below are links to weblogs that reference WeakReference: GC knows the Best:


Kazi Manzur Rashid

Shiplu Bhai, It is really nice to see you blogging frequently, I wish you could start it earlier.

Moim Hossain

Seems I have started keeping any eye on your blog. Its really nice reading ur blogs. and this is also a good post. simple but many of us dont know the details of weak reference.
thanks again :)


Hi Shafqat,

There is a common race condition bug in your code snippet:

if (wr.IsAlive)
Console.WriteLine("Book is alive");\
Book book2 = wr.Target as Book;
book2 = null;

Since the garbage collector runs on a different thread, your wr.IsAlive check could return true, but then your wr.Target as Book could return null, which would then cause a NullReferenceException at runtime.

A correct implementation is:

// Grab a reference to the object, keeping it alive temporarily.
Book book2 = wr.Target as Book;
if (book2 != null){
Console.WriteLine("Book is alive");\
book2 = null;

Shafqat Ahmed

Amit: Thanks a lot for the wishes, I am just trying to grow a writing habbit.

Moim: Great to hear that you are reading my blog, Thanks.

Judah: Yes, I wrote that snippet at that way first, then thought why don't I show the IsAlive use of property. Since GC runs in a different thread, in real life one must cast and check for null. I agree, I will update the snippet later.


Does C# have soft references like Java does? In Java, this would be the prefered approach since a weak reference will be garbage collected even if there is no memory pressure. For a cache, you'd want it to gracefully shrink down as more important tasks require memory and the VM has determined that its becoming a scarce resource. The point of a cache is keep frequently used data, and I think a weak reference approach would have too high of a churn rate be very effective.

Another aspect, at least in Java, is that a reference queue is guarded by a lock (synchronize). If only one queue is used, then this can cause poor performance due to high lock contention. You'd want to have a reference queue per bucket if you built a hashtable-based cache.

Shafqat Ahmed

Unfortunately no. C# does not have soft references. It would have been great to have soft references, since then I could also apply different modes to GC if it had any and customize my requirements of churn.


Nice post.
I found so intriguing the fact that since 2002, you are the first one I know to blog about WeakReference.
This class is a powerfull tool for architectural empowerment, and it's a pity that there isn't so much literature on the web about it.

Shafqat Ahmed

There are some good articles on the web ... I even saw someone using is to implement a .NET cache. But I would loved to see SoftReference implemented in .NET


did you ever work out how to write your example using IsAlive, but avoiding the race condition identified by Judah?
I'm still not convinced it's possible.

Shafqat Ahmed

What Judah said is correct and that is the correct implementation.

Andy Wilson

The MSDN documentation says "Avoid using weak references as an automatic solution to memory management problems. Instead, develop an effective caching policy for handling your application's objects." (http://msdn.microsoft.com/en-us/library/ms404247.aspx)

This is because these are weak reference, not soft references. So, the garbage collector will clean up the cache too often for this approach to be effective. Especially on release builds, the .NET GC can be quite aggressive.

Faisal Zubair

Hi Shafqat h r u , my name is Faisal Zubair i have read your article regarding weak reference but could not get few points which i would like to ask you.When garbage collector runs it will remove all objects whose reference is null and also remove weak reference then why we use weak reference , what is the purpose of it or advantage of it? Please reply as soon as possible.

Best Regards
Faisal Zubair.

Shafqat Ahmed

Use weak references when you think you might need the object in future, but if there is memory pressure they will be removed. So if you have any object which don't need immediately but might need in "near future" ( meaning before gc collects)

So if you have an object loading which is expensive but you dont want to hold on to it. You'd be happy if its there next time you want it, if not you would reload it, then weak reference is your thing.

There are a lot of references in the net on weak ref, it is present in most standard languages like java, python ect.

Robert Bolton

Thanks Shafqat! Very good read and easy to understand. I appreciate you taking the time to post this.


Very interesting article


Thanks. This is very helpful

The comments to this entry are closed.