Export (0) Print
Expand All

WeakReference Class

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

For a list of all members of this type, see WeakReference Members.

System.Object
   System.WeakReference

[Visual Basic]
<Serializable>
Public Class WeakReference
   Implements ISerializable
[C#]
[Serializable]
public class WeakReference : ISerializable
[C++]
[Serializable]
public __gc class WeakReference : public ISerializable
[JScript]
public
   Serializable
class WeakReference implements ISerializable

Thread Safety

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

Remarks

The common language runtime garbage collection mechanism reclaims inaccessible (that is, unreachable) memory allocated to an object. An object becomes unreachable if all references to it become invalid, for example, by setting those references to a null reference (Nothing in Visual Basic).

Garbage collection will call an object's Finalize method (that is, perform finalization), if there is such a method and if instructed to do so. Once finalization is complete, unpredictable results will occur if the garbage collected object is reacquired somehow and reused.

A directly or indirectly referenced object is reachable, and the garbage collector is not permitted to reclaim it. A reference to a reachable object is called a strong reference.

A weak reference also references a reachable object, which is called the target. A user acquires 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 it still has a weak reference.

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 will be retrieved. If the target has not yet been collected, a valid reference will be retrieved.

An object can be used (that is, be alive); become unused and eligible for garbage collection (that is, dead); then used again (that is, resurrected). However, unpredictable behavior results if a resurrected object is accessed after finalization.

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.

Requirements

Namespace: System

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

WeakReference Members | System Namespace | GC | Finalize

Show:
© 2014 Microsoft