Glimpse/Glimpse — Issue #126: Dynamic Assemblies Memory Leak
Author: hendryluk
Created: 2011-10-31 00:46:22+00:00
State: open

We're having memory leak issue on production, which WinDbg reveals to be a leak on dynamcally generated assemblies. (The app generates thousands of dynamic-assemblies before eventually be brought down to its knees).
I eventually tried to uninstall Glimpse, and it seems to have resolved the issue.
After examining the Glimpse source-code, I tracked this down to Glimpse.MVC3 component. In particular, within the Glimpse.Mvc3.Extensions.CreateDynamicProxy.CreateDynamicProxy(IModelBinder, IGlimpseLogger) method.

internal static IModelBinder CreateDynamicProxy(this IModelBinder modelBinder, IGlimpseLogger logger)
{
var proxyConfig = new Dictionary
{
{"BindModel", new BindModelInterceptor()},
{"BindProperty", new BindPropertyInterceptor()},
{"CreateModel", new CreateModelInterceptor()},
};

var proxyGenerator = new ProxyGenerator(); // <- code in question
var proxyGenOptions = new ProxyGenerationOptions(new SimpleProxyGenerationHook(logger, proxyConfig.Keys.ToArray())) { Selector = new SimpleInterceptorSelector(proxyConfig) };
return (IModelBinder)proxyGenerator.CreateClassProxy(modelBinder.GetType(), proxyGenOptions, proxyConfig.Values.ToArray());
}

I notice that it instantiates a new instance of ProxyGenerator every time, which results in a new dynamic type generated every single time, as opposed to reusing what has been previously generated (as in the case when we reuse the ProxyGenerator). Since type/assembly definitions can't be garbage collected, this will result in a memory-leak.
Can this be clarified?
Thank you