WeakReferences And Tracking Resurrection

The WeakReference class has two public constructors.


public WeakReference(Object target)

public WeakReference(Object target, bool trackResurrection)


The first parameter is pretty obvious. It’s the object you want the WeakReference to reference, without keeping the object alive. If a WeakReference is the only thing referencing an object, then the GC is free to collect the object. After the GC collects the object, WeakReference.IsAlive will return false, and WeakReference.Target will return null. The WeakReference loses its handle to the object as soon as the GC collects it but before the finalizer (if any) gets run. This is called a short WeakReference.


If the caller passes true as the trackResurrection parameter, then the WeakReference tracks the object’s life until finalization is complete. This is called a long WeakReference. If the object is resurrected, a long WeakReference will continue to reference it, where a short WeakReference will report it as dead.


The same distinction also applies to GCHandleType.Weak.