views:

138

answers:

2

In another question, Stephen C says:

A second concern is that there are runtime overheads with using weak references. The obvious costs are those of creating weak references and calling get on them. A less obvious cost is that significant extra work needs to be done each time the GC runs.

So what exactly is the cost to the GC of a weak ref? What extra work does it need to do, and how big of a deal is it? I can make some educated guesses, but am interested in the actual mechanics.

+2  A: 

Please check Jeffrey Richter's article about Memory Management in .NET it must clear up things a little.

Incognito
+1 nice article.So basicly the overhead of weak references is only when you have a lot of weak references because the GC has to check if the weak reference'd target is going to be cleaned up, if that is the case, set the weak reference's target to null.In any case, I would find it strange if anyone has that many weak references in their application. Generally you need none.
Stormenet
A: 

What do you mean by "weak references"?

Wardy
Generally, a weak reference is a pointer to a variable which doesn't increase the reference count for that object. This can be helpful for cyclical references - if one of the objects has a weak reference to the other object, and that is the only reference, then the object pointed to by the weak reference will be released, thereby breaking the cycle. This will also lead to the other object being released.
a_m0d
Thanks for the explanation ... I can't say i've ever needed to be in that situation, hense my confusion.I know I lost a few points over it but still worth it for the info.
Wardy