O'Reilly logo

.NET Common Language Runtime Unleashed by Kevin Burton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

WeakReference or Racing with the Garbage Collector

As long as an application root references an object, it is said to have a strong reference to it. As long as an object has a strong reference, the garbage collector cannot collect it. The .NET Framework also has the concept of a weak reference. A weak reference allows the programmer to retain access to an object; however, if the runtime requires memory, the object that is referenced by the weak reference can be collected.

It is all about timing. To use the WeakReference class, you need to follow the next steps:

1.
Allocate memory to be used as a buffer, an object, and so on. The variable that references the allocated memory is now the strong reference to this object.
2.
Construct a WeakReference ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required