It’s common to run into the problem of resources and tasks hanging around after they need to. It’s easy to forget to close a database connection, or cancel a timer, or stop an HTTP request that isn’t needed any more. This can cause all sorts of subtle problems from hanging to leaks and beyond.
Let’s look at different ways to avoid this problem, including the new “using” statement and the resource management tools that come with it, alongside existing APIs. We’ll explore patterns that make it easier to manage resources in the face of unexpected exceptions, and how to diagnose which resources aren’t being closed properly.