Search code examples
asp.netmemory-leaksiis-6connection-poolingapplication-pool

IIS application pool and .NET garbage collection


Consider an ASP.NET application with has a connection pool memory leak problem (where connections are not being closed correctly for example).

Does recycling the application pool clear the connection pool (thus allowing more connections to be made)?

If the connections are left in memory until the Garbage Collector removes them, does this happen when the application pool is restarted (or are/can they left beyond that)? I also understand the Garbage Collector could clean them up at anytime as well, but are they still in use and unable to be collected until a reset or application pool restart?

I'm reviewing a system where the end goal is obviously to have the code corrected to manage the connections correctly, and I'm trying to gain more of an understanding about the garbage collection/application pool process.


Solution

  • Yes, recycling the app pool kills and restarts the IIS process responsible for running your application. All resources are freed at this point, simply because the process is exiting.

    If the process is never restarted and simply leaks handles, the garbage collector will eventually clean them up. However, it's likely that you'll run out of handles for whatever resource is leaking before this happens. This is why it's important to call Dispose() on these objects(preferably by the "using" pattern) so that the resources are freed as soon as the app is done with them and not when the garbage collector gets around to it.