#740 – Short vs. Long Weak References

You can create a WeakReference object that refers to a regular reference-typed object and lets you discover whether the original object has been garbage collected or not.  You can use the Target property of the WeakReference to reference the original object, if it’s still alive.

By default, a WeakReference object creates a short weak reference–the target of the weak reference becomes null and the IsAlive property reports false as soon as the object’s finalizer is called, but potentially before the object is actually garbage collected.

You can also create a long weak reference by passing a value of true as the second parameter to the WeakReference constructor.  The Target will remain non-null after the object has been finalized, up until the point in time when it actually gets garbage collected.  In this way, you retain access to an object that has been finalized, but not collected.

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: