在 ASP.NET MVC 应用程序中使用实体框架和 Windsor 时出现内存泄漏

发布于 2024-10-03 13:09:11 字数 276 浏览 0 评论 0原文

我无法让温莎容器和实体框架一起工作,这可能是由于我自己介绍的一个问题,但最终结果是我遇到了严重的内存泄漏。

我的应用程序设置了 EDMX、存储库和服务,这些内容和 objectcontext 在我使用的温莎配置文件中设置为 perwebrequest。然而,当我查看 ANTS 内存分析器中的内存使用情况时,我发现尽管确认已调用 Dispose,但对象上下文缓存仍被保留为缓存的引用。

每个请求都会有更多的动态代理被困在内存中。如果其他人设法让自己陷入这样的困境,并能为我提供摆脱困境的建议,我将不胜感激。

I'm having trouble getting the windsor container and entity framework working together and it may be due to a problem I've introduced myself but the net result is that I'm getting a terrible memory leaks caused.

I have my application set up with an EDMX and Repositories and Services and those and the objectcontext are set to perwebrequest in the windsor configuration file I use. However when I look at the memory usage in ANTS memory profiler I see that the object context cache is still being held onto as a reference with the cache despite confirming that Dispose has been called.

And each request more dynamic proxies are getting stuck in memory. If anyone else has managed to get themselves in a pickle like this and can offer me advice to get out of it, it would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清旖 2024-10-10 13:09:11

我已经通过将 Windsor 容器的内核上的发布设置更改为:

_container.Kernel.ReleasePolicy = new NoTrackingReleasePolicy();

似乎虽然 Windsor 容器调用了 perwebrequest 组件的 dispose 方法,但它仍然挂在它们的引用上,从而阻止了它们正在被垃圾收集。

在本例中,它所持有的引用的对象是 ObjectContext 类型。不幸的是,尽管处置了该对象,但该对象中缓存的所有动态代理仍然有效,这意味着我的数据库的副本(或至少是我正在访问的部分)被添加到内存中,每个请求都会导致其加速。

I've managed to track down and resolve the problem by changing the release settings on the kernal for the windsor container to:

_container.Kernel.ReleasePolicy = new NoTrackingReleasePolicy();

It seems although the windsor container calls the dispose method of perwebrequest components it still hangs on to a reference of them which prevents them being garbage collected.

In this case the object it was holding a reference to was of type ObjectContext. Unfortunately despite disposing of this object the all the dynamic proxies cached in this object still remain effectively meaning that a copy of my database (or at least the parts I was accessing) was being added to memory each request causing it to ramp up.

染墨丶若流云 2024-10-10 13:09:11

您可能没有正确处理物体。尝试使用“使用”块。

没有看到代码就无法说更多。

You are probably not disposing objects correctly. Try using "Using" blocks.

Cannot say much more without seeing the code.

青柠芒果 2024-10-10 13:09:11

我也有同样的问题。

经过调查,我似乎在控制器工厂中错过了对 _container.Release(controller) 的调用:

    public override void ReleaseController(IController controller)
    {
        _container.Release(controller);

        var disposable = controller as IDisposable;
        if (disposable != null)
        {
            disposable.Dispose();
        }
    }

但是,我也在使用 Windsor 2.1 并且添加 _container.Release(controller) 对我没有任何作用。

更新到 v3.1 后似乎可以工作了。

希望这有帮助。

ps ANTS 内存分析器 - 救星!

I had the same problem.

After investigation it seemed that I was missing a call to _container.Release(controller) in my Controller Factory:

    public override void ReleaseController(IController controller)
    {
        _container.Release(controller);

        var disposable = controller as IDisposable;
        if (disposable != null)
        {
            disposable.Dispose();
        }
    }

However, I was also using Windsor 2.1 and adding _container.Release(controller) did not do anything for me.

After updating to v3.1 it seems to work.

Hope this helps.

p.s. ANTS Memory Profiler - lifesaver!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文