Export (0) Print
Expand All

WeakReference Class

Represents a weak reference, which references an object while still allowing that object to be garbage collected.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

'Declaration
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class WeakReference
	Implements ISerializable
'Usage
Dim instance As WeakReference

/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class WeakReference implements ISerializable
SerializableAttribute 
ComVisibleAttribute(true) 
public class WeakReference implements ISerializable

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.

There can be a delay between the time an object becomes eligible for garbage collection and the time it is collected. If the user attempts to retrieve the target after it has been collected, only a null reference is retrieved. If the target has not yet been collected, a valid reference is retrieved.

Garbage collection calls an object's Finalize method (that is, performs finalization), if there is such a method and if instructed to do so. Once finalization is complete, InvalidOperationException is thrown if the user attempts to access the object with the IsAlive or Target properties of the current WeakReference object.

Terminology

It is useful to know some of the jargon used to describe objects and garbage collection. An object that is accessible and can be used is said to be alive. An object that is unused and eligible for garbage collection is said to be dead. If a strong reference is acquired to a dead object, the object becomes alive again and the object is said to be resurrected.

A WeakReference object can specify whether the reference to its target be maintained after finalization (that is, whether the weak reference should track the target's resurrection). A weak reference that does not track resurrection is called a short weak reference, and a weak reference that tracks resurrection is called a long weak reference.

Performance Considerations

The WeakReference class is frequently cited in code examples where a cache of WeakReference objects maintains references to a set of objects used by an application. If, for example, the object dies because the object falls out of scope, the garbage collection mechanism can still reclaim the allocated memory.

This example is used to illustrate a clever memory management scheme, but it is misleading because the WeakReference class cannot be treated as an automatic solution to memory management problems. You still have to establish rules for your application, that is, a caching policy, about what entries are kept or removed from a cache.

System.Object
  System.WeakReference

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft