为什么 ASP Net Core 2.2 不释放内存? [英] Why ASP Net Core 2.2 do not release memory?

查看:40
本文介绍了为什么 ASP Net Core 2.2 不释放内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下项目制作的默认项目测试 ASP Net Core 2.2:

Visual Studio > 文件 > 新建 > 项目 > ASP NET Core Web 应用程序 > 下一步 > 创建.按界面上的IIS Express按钮,自动转到

原来这个问题在 2 年前就存在了,没有解决方案,并且有现实生活示例体验https://github.com/aspnet/AspNetCore/issues/1976

我很确定黄色箭头表示 GC 工作正常,但我的进程内存使用量并没有在每个请求中下降.我不确定这是来自 ASP Net Core 的功能还是错误,我预计它会在请求后下降/每次用户获取/values 时都不会增加

编辑:我不希望人们危险地向我的应用发送垃圾邮件,并且由于内存泄漏它开始崩溃.如果此解决方案尚未修复,我很害怕为我的公司使用 Net Core.

为什么 ASP Net Core 2.2 不释放内存?

我尝试在两台不同的笔记本电脑上进行测试,结果是一样的.

编辑:我的同事也有这个问题.

编辑:我尝试使用 dot net core v2.1,结果是一样的.

我试过 GCCollect();结果是一样的.

旧图片参考

启动进程内存

进程内存不断上升

解决方案

ASP.NET Core 使用的内存似乎比应有的多,因为它被配置为默认使用服务器 GC(而不是工作站 GC).有关类似问题的讨论,请参阅 这里.理论上,当您的服务器面临内存压力时,应用程序应该能够减少其内存占用.

此 MSDN 文档Github CoreCLR文档.

要查看是否确实是服务器 GC 导致了额外的内存消耗,您可以在 csproj 文件中将 GC 策略设置为工作站:

<ServerGarbageCollection>false</ServerGarbageCollection></PropertyGroup>

I'm testing ASP Net Core 2.2 using default project made from:

Visual Studio > File > New > Project > ASP NET Core Web Application > Next > Create. Press IIS Express button on interface and automatically go to https://localhost:44356/api/values

Edit: The point is, I'm testing ASP Net Core in case of brute force spamming from people.

I have only modified this:

        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            return new string[] { "value1", "value2" };
        }

Edit: to this, I edited my code to reasonable test, producing minimal reproducable example

// GET api/values

[HttpGet]
public ActionResult<IEnumerable<Post>> Get()
{
    // Testing for brute force
    return Enumerable.Range(0, 100).Select(x => "Example Value").ToList();
}

The starting Process Memory is 80 MB.

However, when I tried refreshing the page, the starting memory keeps coming up:

Edit: Turns out this issue has been there from 2 years ago, with no solution, and with Real Life Example Experience, https://github.com/aspnet/AspNetCore/issues/1976

I'm pretty sure that yellow arrow means GC is working correctly, but my process memory usage isn't coming down every request. I'm not sure if this is a feature or bug from ASP Net Core, I expected it to go down after request / did not increase everytime a user GET the /values

Edit: I don't want people dangerously spamming my app and it starting to crash down because memory leak. And I'm afraid to use Net Core for my company if this solution is not fixed yet.

Why ASP Net Core 2.2 do not release memory?

I have tried testing in two different laptop, the result is same.

Edit: My colleague also have this problem.

Edit: I have tried using dot net core v2.1, the result is same.

I have tried GCCollect(); the result is same.

old image ref.

Starting Process Memory

Process Memory keeps coming up

解决方案

ASP.NET Core can appear to use more memory than it should because it's configured to use Server GC by default (as opposed to Workstation GC). For a discussion of a similar concern see here. In theory, the application should be able to reduce its memory footprint when your server faces a memory pressure.

More on this topic in this MSDN doc and Github CoreCLR doc.

To see if it's indeed Server GC that causes the additional memory consumption, you can set GC strategy to workstation in your csproj file:

<PropertyGroup> 
    <ServerGarbageCollection>false</ServerGarbageCollection>
</PropertyGroup>

这篇关于为什么 ASP Net Core 2.2 不释放内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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