Memory leak when using a custom MVC controller factory in a specific manner


Description

When using a custom MVC controller factory on the Sitecore solution in a specific manner, the web site may experience excessive memory usage.

The issue is caused by specifics of Sitecore implementation of using custom MVC controllers.

Solution and technical details leading to this behavior are available in the sections below.

Solution

To resolve the issue, apply the following patch:

  1. Copy the Sitecore.Support.438688.dll assembly into the /bin folder of your solution.
  2. Copy the Sitecore.Support.438688.config file into the /App_config/Include folder of your solution.

Technical Details

The issue can generally be identified by reviewing the memory dump file of the web application and when locating multiple .NET objects GC rooted by the custom MVC controller factory objects.

Sitecore uses chain of responsibility pattern to register several types of controller factories for different types of requests.

When a custom controller factory is registered (for example, on the application start event), it is placed at the end of processing order:

SitecoreControllerFactory -> TagInjectionControllerFactory -> custom controller factory

After a call to the Execute() method for a controller completes, the ReleaseController() method in the ProcessRequest() method of MvcHandler class is called.

SitecoreControllerFactory calls ReleaseController() method of its inner factory - TagInjectionControllerFactory. In turn, TagInjectionControllerFactory calls controller's Dispose() method. However, it does not call ReleaseController() method of the custom controller factory.

This results in a memory leak in the situations when custom controller factory still tracks the controller object.

For example, such situation can occur if Castle Windsor with its LifecycledComponentsReleasePolicy is used to create controllers.