繁体   English   中英

ASP.NET 核心 ILogger Memory Windows 10 (1607) / ZAEA2342289CE3AA9B340Z6 服务器泄漏

[英]ASP.NET Core ILogger Memory Leak on Windows 10 (1607) / Windows Server 2016

TL;博士

I'm pretty sure now that there is a memory leak in the conhost process in Windows 10 (version 1607) and Windows Server 2016 that hasn't been there in Windows Server 2012 R2 and is fixed in Windows 10 (version 1809+) and Windows 服务器 2019。

我找不到对该错误的任何官方参考,如果有人可以为我指出这一点,我将不胜感激。

很长的故事

我目前正在调查我们的(自托管)ASP.NET 核心应用程序中的内存泄漏,导致 memory 使用率很高,而不是.exe 本身,而是相应的“conhost”进程。 这只会发生在所有生产服务器/虚拟机上,运行相同的应用程序我的笔记本电脑(或任何开发笔记本电脑)不会产生 memory 泄漏。

开发机器(现在运行 > 2 小时): 在此处输入图像描述

生产服务器(运行约 10 分钟): 在此处输入图像描述

那个 memory 会无限堆积,直到服务器用完 memory。

那是我用来复制它的代码(再次:在我们的生产系统上,而不是在开发机器上)。 在我们的实际应用程序中,实际上是 EFCore 日志记录导致问题(EFCore 也使用 ASP.NET 核心记录器):

public class LoggingBackgroundService : BackgroundService
{
    private readonly ILogger<LoggingBackgroundService> logger;

    public LoggingBackgroundService(ILogger<LoggingBackgroundService> logger)
    {
        this.logger = logger;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        for (var i = 0; i < 10; i++)
        {
            Task.Run(() => RunBackgroundThread(stoppingToken));
        }

        await Task.Delay(Timeout.Infinite, stoppingToken);
    }

    private async Task RunBackgroundThread(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            for (var i = 0; i < 1000; i++)
            {
                logger.LogInformation(GetLogMessage());
                //Console.WriteLine(GetLogMessage());
            }

            await Task.Delay(1000);
        }
    }

    private string GetLogMessage()
    {
        return DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.ffff");
    }
}

使用Console.WriteLine()代替记录器可以工作,并且不会产生 memory 泄漏。 再一次,该代码在开发机器上运行良好。

我们使用的是 .NET Core 3.1。 谁能解释这里发生了什么?

编辑:我的测试系统目前位于 ~9 GB RAM,所以也许有一个限制,但无论如何在“conhost”进程上花费的 RAM 太多了。 在此处输入图像描述

我还尝试使用 DebugDiag 来分析 memory 转储,但这显示了一个对我没有帮助的堆栈跟踪(还有什么可以期待的?):

LeakTrack+13277       
ConhostV2!MergeAttrStrings+b8       
ConhostV2!WriteCharsLegacy+a9c       
ConhostV2!DoSrvWriteConsole+4ea       
ConhostV2!ConsoleIoThread+36b       
kernel32!BaseThreadInitThunk+14       
ntdll!RtlUserThreadStart+21 

我对虚拟机只有基本的了解,但目前看起来它只发生在 VMWare ESX (?) 系统中? 我不再认为 VMWare 是导致问题的原因,而是被不同的 Windows 10 版本/构建所误导。

  • [泄漏] Windows 服务器 2016(在 VMWare ESX 上)
  • [泄漏] Windows 10、1607(在 VMWare ESX 上)
  • [泄漏] Windows 10、1607(在 Azure 上)
  • [泄漏] Windows 服务器 2016(在 Azure 上)
  • [泄漏] Windows 服务器 2016(在 AWS 上)
  • [泄漏] Windows 10、1607(在 Hyper-V 上)
  • [泄漏] Windows 服务器 2016(在 Hyper-V 上)
  • [无泄漏] Windows 10、1909(在 VMWare 工作站上)
  • [无泄漏] Windows 服务器 2019(在 Azure 上)
  • [无泄漏] Windows 10、1909(在 Hyper-V 上)
  • [无泄漏] Windows 服务器 2019(在 Hyper-V 上)
  • [无泄漏] Windows 10、1809(在硬件上)

我向微软报告了这个错误,它已经被修复了。

... 2021 年 5 月 Windows 服务器 2016 更新

2021 年 5 月 Windows 服务器 2016 更新 (KB5003197) 中包含一个修复程序,但隐藏在功能标志后面。 激活它

  1. 安装 KB5003197
  2. 添加此注册表项: reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Policies\Microsoft\FeatureManagement\Overrides /v 1402424459 /t REG_DWORD /d 1 /f
  3. 重启系统

... 2021 年 6 月 Windows 服务器更新:

在不使用注册表项的情况下,此修复程序应该在 June-Update 中处于活动状态,但我尚未对此进行测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM