Linq to SQL DataContext Windsor IoC memory leak problem

Posted by Mr. Flibble on Stack Overflow See other posts from Stack Overflow or by Mr. Flibble
Published on 2009-11-10T20:32:54Z Indexed on 2010/05/18 11:30 UTC
Read the original article Hit count: 466

I have an ASP.NET MVC app that creates a Linq2SQL datacontext on a per-web-request basis using Castler Windsor IoC.

For some reason that I do not fully understand, every time a new datacontext is created (on every web request) about 8k of memory is taken up and not released - which inevitably causes an OutOfMemory exception.

If I force garbage collection the memory is released OK.

My datacontext class is very simple:

 public class DataContextAccessor : IDataContextAccessor
 {
    private readonly DataContext dataContext;
    public DataContextAccessor(string connectionString)
    {
        dataContext = new DataContext(connectionString);           
    }
    public DataContext DataContext { get { return dataContext; } }
 }

The Windsor IoC webconfig to instantiate this looks like so:

 <component id="DataContextAccessor"
             service="DomainModel.Repositories.IDataContextAccessor, DomainModel"
             type="DomainModel.Repositories.DataContextAccessor, DomainModel"
             lifestyle="PerWebRequest">       
    <parameters>
      <connectionString>
        ...
      </connectionString>
    </parameters>
  </component>

Does anyone know what the problem is, and how to fix it?

© Stack Overflow or respective owner

Related posts about inversion-of-control

Related posts about castle-windsor