LINQ到SQL的DataContext温莎IoC的内存泄漏问题

LINQ到SQL的DataContext温莎IoC的内存泄漏问题

本文介绍了LINQ到SQL的DataContext温莎IoC的内存泄漏问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用Castler温莎的IoC。

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

由于某种原因,我不完全理解,每一个新的datacontext创建时间(每个web请求)的内存大约8K被吸收而不是释放 - 这不可避免地会导致内存不足异常。

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.

我的DataContext类是非常简单的:

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; } }
 }

温莎国际奥委会webconfig实例,这看起来像这样:

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?

推荐答案

L2S的DataContext实现了IDisposable。你的界面也有实现它,并调用DataContext.Dispose(),以便温莎知道有正在资源被布置

L2S DataContext implements IDisposable. Your interface also has to implement it, and call DataContext.Dispose(), so that Windsor knows that there're resources to be disposed.

顺便说一句提防温莎/ IDisposable的问题:

By the way beware of Windsor/IDisposable problems:http://www.jeremyskinner.co.uk/2008/05/03/aspnet-mvc-controllers-windsor-and-idisposable/http://www.nablasoft.com/Alkampfer/?p=105

这篇关于LINQ到SQL的DataContext温莎IoC的内存泄漏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 06:13