Wednesday, September 20, 2006

Weak References in .NET

Did you know that WeakReference class represents a weak reference, which references an object while still allowing that object to be garbage collected?
The common language runtime garbage collection mechanism reclaims inaccessible objects from memory. An object becomes inaccessible if all direct and indirect references to that object become invalid (for example, by setting those references to a null reference (Nothing in Visual Basic)). A reference to an accessible object is called a strong reference.
A weak reference also references an accessible object, which is called the target. A user creates a strong reference to the target by assigning the value of the Target property to a variable. However, if there are no strong references to the target, the target becomes eligible for garbage
collection even though there is a weak reference to the object.

No comments: