We are experiencing a huge memory leak with our ASP .NET application. It uses EF 6 and Castle Windsor. After the application has been running for several hours, we can easily see that the physical memory steadily climbs with no end in sight. An application restart is our only savior before the application starts timing out.
As you can see from the image below, the memory is taken up by the custom DB context.
I think the issue is with the registration of the controller. Everything in the IoC is registered as LifestylePerWebRequest except for the controller which is registered as LifestyleTransient. If I try to register the controller as LifestylePerWebRequest, I get an error like the one below. I think the container is holding on to all of these instances and not knowing when to let them go.
A single instance of controller 'Acme.Web.Controllers.HomeController' cannot be used to handle multiple requests. If a custom controller factory is in use, make sure that it creates a new instance of the controller for each request.
How can I resolve this so that the DB Context is cleaned up along with the rest of the hierarchy?
I think I've found the answer. I had come across the WindsorControllerFactory before but I didn't put it to use. Now that it's in place and registered as shown below, the container is releasing the controller. Now that it's being released it get picked up for garbage collection. It seems to be working really well so far.
var controllerFactory = new WindsorControllerFactory(container.Kernel); ControllerBuilder.Current.SetControllerFactory(controllerFactory);