从未调用过Ninject.MVC5发布范围-OutOfMemoryException [英] Ninject.MVC5 Release Scope Never Called - OutOfMemoryException

查看:98
本文介绍了从未调用过Ninject.MVC5发布范围-OutOfMemoryException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Ninject的新手,但我已经确信Ninject.MVC5 NuGet包的开箱即用配置实际上从未释放范围为HttpContext的对象.在一些较大的报告中反复使用大量内存后,我始终收到OutOfMemoryException.

I'm new to Ninject, but I've become convinced that the out of the box configuration for Ninject.MVC5 NuGet package never actually releases the objects scoped to the HttpContext. I consistently get an OutOfMemoryException after a bout of repeated heavy memory usage by some of my larger reports.

因此,为了证明我没有失去理智,我创建了一个空的测试项目.我在VS 2017中通过文件->新建项目-> C#-> Web-> ASP.NET Web应用程序-> MVC(在模式中)创建了一个默认项目.显然,这也是一个新的解决方案.然后,我安装了Ninject 3.2.2(不是最新版本; Ninject.MVC5要求<3.3),然后安装了Ninject.MVC5.

So to prove I wasn't losing my mind I created an empty test project. I created a default project in VS 2017 by File -> New Project -> C# -> Web -> ASP.NET Web Application -> MVC (in the modal). This is a new solution too, to be clear. I then installed Ninject 3.2.2 (not the latest; <3.3 is required for Ninject.MVC5), then installed Ninject.MVC5.

然后我转到HomeController.cs,首先在文件底部添加一个类:

Then I went to HomeController.cs and first added a class to the bottom of the file:

public class MemoryEater : IDisposable
{
    public List<int> _nums = new List<int>();

    public MemoryEater()
    {
        var rand = new Random(); // Use random so that I get a bit of CPU usage I can see in Task Manager
        for (int cnt = 0; cnt < 2_500_000; ++cnt)
        {
            var key = rand.Next();
            _nums.Add(key);
        }
    }

    public void Dispose()
    {
        if (_nums != null)
            _nums = null;
    }
}

然后我向HomeController添加了一个构造函数:

Then I added a constructor to HomeController:

public class HomeController : Controller
{
    readonly MemoryEater _me;

    public HomeController(MemoryEater me)
    {
        _me = me;
    }

最后我打开了NinjectWebCommon.cs,直到现在我都没有对其进行任何修改(此文件通过通过NuGet安装Ninject.MVC5创建了100%的创建),并在RegisterServices中添加了一行:

And finally I opened up NinjectWebCommon.cs, which I didn't modify in any way up until now (this file created 100% by installing Ninject.MVC5 via NuGet), and added one line to RegisterServices:

    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<MemoryEater>().ToSelf().InRequestScope();
    }    

然后我使用IISExpress运行该应用程序,并在Chrome中查看(在Windows 10上).我刷新页面,观察内存使用率的上升;随着内存使用率的下降,某些GC会清楚地发生,这可能是由于List对象必须增长.但是它不断攀升到600MB以上,经过几十次刷新后,我的应用程序中出现了OutOfMemoryException.

I then run the app with IISExpress and viewed in Chrome (on Windows 10). I refresh the page and watch the memory usage climb; some GCs happen clearly as the memory usage drops, which is expected b/c the List object has to grow. But it keeps climbing past 600MB and, after a few dozen refreshes, I get an OutOfMemoryException in my app.

因此,我走了一步,将GitHub的OnePerRequestHttpModule.cs的源代码复制到了我的项目中的新文件中,并拆分了DeactivateInstancesForCurrentHttpRequest方法,以便可以在ICache.Clear调用处放置一个断点:

So I went a step further and copied the source from GitHub for OnePerRequestHttpModule.cs into a new file in my project, and split up the method DeactivateInstancesForCurrentHttpRequest so that I could put a breakpoint where ICache.Clear is called:

    public void DeactivateInstancesForCurrentHttpRequest()
    {
        if (this.ReleaseScopeAtRequestEnd)
        {
            var context = HttpContext.Current;
            this.MapKernels(kernel => ClearCacheOfContext(kernel, context)); // BREAKPOINT A
        }
    }

    private void ClearCacheOfContext(IKernel kernel, HttpContext context)
    {
        kernel.Components.Get<ICache>().Clear(context); // BREAKPOINT B
    }

我将NinjectWebCommon更改为使用新的OnePerRequestHttpModule副本,并将断点放在A和B处.在每次请求A被调用之后,B被从未调用.

I changed NinjectWebCommon to use my new copy of OnePerRequestHttpModule and put breakpoints at A and B. After every request A is called but B is never called.

我做错什么了吗,或者这是一个合法的错误?在我看来,这个错误可能存在,并且我(Ninject的n00b)可能是第一个找到它的人.

Am I doing something wrong or is this a legit bug? It seems unthinkable to me that this bug could exist and me, a n00b to Ninject, could be the first one to find it.

推荐答案

仔细查看您的Ninject.Web.Common nuget软件包的版本.

Have a closer look at the version of your Ninject.Web.Common nuget package.

如果该值小于3.2.2,则可能是导致问题的原因.

If it is less than 3.2.2, this is the cause of your issue.

此错误已在3.2.2版中修复

这篇关于从未调用过Ninject.MVC5发布范围-OutOfMemoryException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆