Take the 2-minute tour ×

So I've been mulling over some automatic memory management ideas lately - specifically, I've been looking at implementing a memory manager based on reference counting. Of course, everyone knows that circular references kill naive reference counting. The solution: weak references. Personally, I hate using weak references in this way (there are other more intuitive ways of dealing with this, via cycle detection), but it got me thinking: where else could a weak reference be useful?

I figure that there must be some reason they exist, especially in languages with tracing garbage collection, which do not suffer from the cyclic reference pitfall (C# and Java are the ones I'm familiar with, and Java even has three kinds of weak references!). When I've tried to find some solid use-cases for them, though, I pretty much just got ideas like "Use them to implement caches" (I've seen that a few times on SO). I don't like that either, since they rely on the fact that a tracing GC will likely not collect an object immediately after it's no longer strongly referenced, except in low-memory situations. These kinds of cases are absolutely invalid with reference counting GC since an object is destroyed immediately after it is no longer referenced (except possibly in the case of cycles).

But that really leaves me wondering: How can a weak reference possibly useful? If you can't count on it referencing an object, and its not needed for things like breaking cycles, then why use one?

share|improve this question
Of course, everyone knows that a reference is a reference at the application level. Either you're using the object in question, or you don't care whether it's garbage collected or not. Weak references are ONLY of interest if you're working at the GC level. Here's a good article, if you're not already familiar with it: weblogs.java.net/blog/2006/05/04/understanding-weak-references –  paulsm4 Aug 21 '11 at 5:47
"with reference counting GC since an object is destroyed immediately after it is no longer referenced". In practice, reference counts are decremented when variables fall out of scope which can be a long time after the object they refer to was last referenced. The idea that reference counting destroys objects as early as possible is a common misconception. –  Jon Harrop Jan 10 '13 at 20:30
@JonHarrop: That's a good clarification. I tend to use the word "referenced" as "the object is bound to a variable" rather than "the object is being used", but I can see how this could be interpreted differently. –  Ken Wayne VanderLinde Jan 10 '13 at 22:03
@KenWayneVanderLinde Right. Perhaps "reachable" would be better. Tracing garbage collectors like OCaml's can and do collect values earlier than scope-based reference counting collection like shared_ptr in C++. –  Jon Harrop Jan 11 '13 at 0:11
add comment

2 Answers

up vote 9 down vote accepted

Event handlers are a good use case for weak references. The object that fires events needs a reference to the objects to invoke event handlers on, but you typically don't want the event producer's reference holding to prevent the event consumers from being GC'd. Rather, you'd want the event producer to have a weak reference, and it would then be responsible for checking whether the referenced object was still present.

share|improve this answer
Hey, never thought about a case like that! –  Ken Wayne VanderLinde Aug 21 '11 at 5:59
They'd be a better use case for weak references if there were a delegate type which would not keep the target alive, but would invalidate itself if the target were collected, and if Delegate.Combine knew enough about this delegate type to skip invalidated instances when combining delegates. Otherwise the .net event pattern has no good remedy for a scenario where many short-lived objects are created, subscribed to events from long-lived objects, and abandoned. –  supercat Dec 11 '11 at 17:40
@supercat: Well, this isn't a question about .Net, or about any other platform or language - it's just about places where weak-references could be useful. Ironically, the project I was working on was in C++ (no event model), and it involved both reference counting and event handlers! –  Ken Wayne VanderLinde Jan 16 '12 at 19:26
add comment

Object referenced by WeakReference will be accessible before the gc process.

So if we wanna have the information of the object as long as it exists, we can use WeakReference. For example, Debugger and Optimizer will often need to have the information of an object but they don't want to affect the GC process.

By the way, SoftReference is different from WeakReference because the related object will be collected only when the memory is not enough. So, SoftReference will be used to build global cache usually.

share|improve this answer
Yeah, you got the point. –  user898756 Aug 21 '11 at 6:08
Actually, the last part about SoftReference will not work with reference counting GC as objects are collected regardless of the memory situation. But I do like that you clarified that this works reliably for languages with tracing GC. –  Ken Wayne VanderLinde Aug 21 '11 at 6:10
add comment

Your Answer


