What happens to the WeakReference when the target object referenced by WeakReference.Target has been garbage collected? Does the WeakRerence stay alive and keeps existing? The reason why I am asking is that I have a list of WeakReferences stored in a List. During runtime new WeakReferences constantly are getting added to that list. Now when the target object dies, do I have to cleanup the abandoned WeakReference myself? If so, is there a clever trick how I could do this? Can I get notified when a WeakReference becomes abandoned? Or do I have to introduce a timer that frequently loops through that list, to see if any WeakReference instances can be removed from that list.
Weak references are all about garbage collection. A standard object will not "disappear" until all references to it are severed, this means all the references your various objects have to it have to be removed before garbage collection will consider it garbage.
Weak reference objects, which do not prevent their referents from being made finalizable, finalized, and then reclaimed. Weak references are most often used to implement canonicalizing mappings. Suppose that the garbage collector determines at a certain point in time that an object is weakly reachable.
A weak reference is one that references an object in the memory while at the same time enabling the garbage collector to collect the object or reclaim the memory occupied by the object when the GC runs. An object that is reachable isn't garbage collected by the runtime.
A weak reference is a way to have some pointer to an object that does have any reference (strong reference). In . NET, any normal reference to another object is a strong reference . That is, when you declare a variable of a type that is not a primitive/value type, you are declaring a strong reference.
This is a common problem with weak references. The reference itself remains alive because it has normal pointers to it. As you suggest, you need to do some "manual garbage collection" from time to time. Note that you can probably clean up the stubs on your way when you are traversing the list for another reason. Depending the use pattern for the list, this "on the side" garbage collection may even be enough.
Do not "frequently" loop through the list for the sole purpose of cleaning it up! Each dead stub only wastes a couple of words of memory. If the list is not used often the computational cost of cleaning it often is not justified, and if it is used frequently it will clean itself up as suggested above.
It's in another garbage-collected system altogether, but the problems are so similar that you may be interested in this article if you can get it.
Since you have a strong reference to the WeakReference object, it will not get GC'ed. This is also by design, because it was intended that you can still use the WeakReference to find out that the target has been GC'ed.
So yes, you'll have to go the timer way.
Added: You might also take a look at Garbage Collection Notifications.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With