-
Notifications
You must be signed in to change notification settings - Fork 413
Dynamic Proxy Memory Leak #126
Description
We were experiencing a memory leak issue on production, which WinDbg unveals to be a leak on dynamcally generated assemblies. (Thousands of dynamic-assemblies are loaded onto the process before eventually bringing the server down to its knees, causing our Azure instances to reboot).
After we eventually tried to uninstall Glimpse, the issues seem to have been resolved.
Upon examining Glimpse source-code, I narrowed my suspicion down to the Glimpse.MVC3 component. In particular, within the Glimpse.Mvc3.Extensions.ModelBinderExtensions.CreateDynamicProxy(IModelBinder, IGlimpseLogger) method.
internal static IModelBinder CreateDynamicProxy(this IModelBinder modelBinder, IGlimpseLogger logger)
{
var proxyConfig = new Dictionary<string, IInterceptor>
{
{"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());
}
The method 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 instance). Since type/assembly definitions can never be garbage collected in .net, this will result in a memory-leak.
Can this be looked into?
Thanks in advance