Windows 10 (1607)/Windows Server 2016 上的 ASP.NET Core ILogger 内存泄漏 [英] ASP.NET Core ILogger Memory Leak on Windows 10 (1607) / Windows Server 2016

查看:36
本文介绍了Windows 10 (1607)/Windows Server 2016 上的 ASP.NET Core ILogger 内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在很确定在 Windows 10(版本 1607)和 Windows Server 2016 中的 conhost 进程中存在内存泄漏,而在 Windows Server 2012 R2 中没有,并且在 Windows 10(版本 1809)中已修复+) 和 Windows Server 2019.

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 Server 2019.

我找不到有关该错误的任何官方参考,如果有人能指出这一点,我将不胜感激.

I couldn't find any official reference to that bug, if anyone can point that out for me I'd appreciate it.

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

I'm currently investigating a memory-leak in our (self-hosted) ASP.NET Core Application that results in a high memory usage not of the .exe itself but of the corresponding 'conhost' process. That will only happen on all production servers / VMs, running the same application my laptop (or any dev laptop) does not produce a memory leak.

Dev-Machine(现在运行 2 小时以上):

Dev-Machine (running now for > 2h):

生产服务器(运行约 10 分钟):

Production Server (running for ~ 10min):

该内存会无限堆积,直到服务器内存耗尽.

That memory will pile up indefinitely until the server runs out of memory.

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

That is the code I'm reproducing it with (again: on our production systems, not on dev machines). In our real application it is actually EFCore logging causing the issue (EFCore also uses the ASP.NET Core Loggers):

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() 代替记录器可以工作并且不会产生内存泄漏.再一次,该代码在开发机器上运行良好.

Using Console.WriteLine() instead of the logger works and will not produce a memory leak. And again one more time, that code works fine on a dev machine.

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

We are using .NET Core 3.1. Can anyone explain what's going on here?

我的测试系统目前位于 ~9 GB RAM,所以可能有一个限制,但无论如何在conhost"进程上花费了太多的 RAM.

My test-system currently sits at ~9 GB RAM, so maybe there is a limit but that's too much RAM spent on a 'conhost' process in any case.

我还尝试使用 DebugDiag 来分析内存转储,但它显示的堆栈跟踪并没有真正帮助我(还有什么可以期待的?):

I also tried to use DebugDiag to analyze a memory dump, but that shows a stack trace that doesn't really help me (what else to expect though?):

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

我只对 VM 有基本的了解,但目前看起来它只发生在 VMWare ESX (?) 系统中? 我不再认为 VMWare 是导致问题的原因,而是被不同的Windows 10 版本/内部版本.

I only have a basic understanding of VMs but currently it looks like it is only happening in VMWare ESX (?) systems? I no longer think VMWare is causing the issue but was misled by the different Windows 10 Versions / Builds.

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

推荐答案

我向 Microsoft 报告了此错误,此错误已得到修复.

I reported this bug to Microsoft and it has since been fixed.

... 2021 年 5 月的 Windows Server 2016 更新:

2021 年 5 月的 Windows Server 2016 更新 (KB5003197) 中已包含修复程序,但隐藏在功能标志后面.激活它

A fix has been included in the May 2021 Windows Server 2016 update (KB5003197) but is hidden behind a feature flag. To activate it

  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 Server 更新:

此修复程序应该在六月更新中处于活动状态,而无需使用注册表项,但我尚未对此进行测试.

This fix should be active with the June-Update without using the registry entry but I haven't tested this.

这篇关于Windows 10 (1607)/Windows Server 2016 上的 ASP.NET Core ILogger 内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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