Search code examples
structuremapopenrasta

Memory leaks in OpenRasta service when using StructureMap DI


We are using OpenRasta for a service to serve up binary resources (images and PDFs mostly). Some of these are relatively large (5-75MB). We were encountering issues with performance, and, using the ANTS memory profiler, determined that there appeared to be memory leaks.

We have been using StructureMap for DI, and found the following modification in a blog post:

public void HandleIncomingRequestProcessed()
{
    HttpContextLifecycle.DisposeAndClearAll();
}

The leaks went away when we made a the further modification:

public void HandleIncomingRequestProcessed()
{
    HttpContextLifecycle.DisposeAndClearAll();
    ObjectFactory.EjectAllInstancesOf<IRequest>();
    ObjectFactory.EjectAllInstancesOf<IResponse>();
    ObjectFactory.EjectAllInstancesOf<ICommunicationContext>();
}

EDIT: This is not a good idea, as it will mess up concurrent requests. See the comments to the answer.

Essentially, I want to know, will this screw anything up? Or is it a pull request worth submitting?

Here are the before/after pics from the profiler:

Before

After

Thanks-


Solution

  • Do a pull request. The SM support is poor and the context store is not in use, hence the problems you're seeing (amongst other problems that is).

    That said, wouldn't evicting all instances of IRequest remove all the ones in the http context SM uses? You may want to check the documentation there.