I am experiencing memory leaks in an ASP.Net MVC 3 application and I suspect it may be an issue with the IoC container.
The MvcApplication creates a WindsorContainer object, populates it and then stores it in a static field like this:
public class MvcApplication : System.Web.HttpApplication, IContainerAccessor
{
private static WindsorContainer container;
protected void Application_Start()
{
ControllerBuilder.Current.SetControllerFactory(
new WindsorControllerFactory(container));
}
}
Is it correct here to hold on to the container in a static field? As far as I understand, the container itself only needs to live as long as the MVCApplication itself. Making it static would simply share it across multiple MVCApplication instances, so I wonder if it is being disposed incorrectly.
Container can be created this way and stored in the application object as a static field. You can control the lifetime of objects create by the container (see this page). For a web application the usual lifetime is per request – example copied from the aforementioned page:
And in this case, you will never have to share it across multiple instances of
MvcApplicationclass – there’s usually one implementation ofHttpApplicationclass in web application.