记一次生产环境asp.net core内存暴增以及无法释放导致应用宕机

环境: PCF(AWS)

在production环境上传几张图片后,app 内存暴增以及无法释放导致crashed in a short time

解决方案

  1. 解决内存不能主动释放问题 - 项目文件增加了GC工作模式  相关文档https://www.iaspnetcore.com/blog/blogpost/5a38af052d485225f4f40be9/net-core-two-kinds-of-gc-mode-server-gc-workstation-gc-settings

         

          

         2. 解决内存暴增的问题 - 在管道中增加个GC回收的中间件

public class GCMiddleware
    {
        private readonly RequestDelegate _next;

        public GCMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext httpContext)
        {
            await _next(httpContext);
            GC.Collect(2, GCCollectionMode.Forced, true);
            GC.WaitForPendingFinalizers();
        }
    }

效果:

程序刚启动

  

连续几十次request之后:

等待几分钟后:


海的呐喊
原文地址:https://www.cnblogs.com/kejie/p/15157364.html