Object Disposal

Now I want you to look outside the reach of the garbage collection itself and focus on object C in the heap. At the point of the collection, C is not reachable anymore because there’s no single path through object references that can lead to it from a root, which is a location known to be reachable. Apparently, there was an object D holding on to it, but that object by itself is not reachable anymore.

What we don’t know is at what point in the past object C was allocated. It could be a few milliseconds ago, but it could also be hours or more (assuming, for instance, the application was idle waiting for user input). If C holds on to some resource that should not be kept alive needlessly (like a database connection or a file handle), ...

Get C# 5.0 Unleashed 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.