Weak References

C# and Java both define weak references. If the only reference to an object is a weak reference, the object can be collected as garbage. Weak references are useful for referencing data that can easily be re-created and that should be freed if there is pressure for memory. Weak references are frequently used for cached data, where there is a performance improvement when the data is read from the cache but the data can be re-created if the cached information is collected.

The System.WeakReference class is used to create a weak reference. A weak reference is said to track an object, which is to say, maintain a weak association. There are two forms of weak reference, short-lived and long-lived. The difference is that long-lived weak ...

Get C# for Java Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.