aspnetcore: Memory leak when restarting host on aspnet core 5

Describe the bug

After calling IHostApplicationLifetime.StopApplication() I am trying to build and start the IHost again. Inspecting the diagnostic tools on visual studio I can see that the memory goes up (a bit) every cycle, even trying to call System.GC.Collect().

To Reproduce

public class Program
{
    public static void Main(string[] args)
    {
        while (true)
        {
            using (var builder = CreateHostBuilder(args).Build())
            {
                builder.Start();
                builder.WaitForShutdown();
            }
            System.GC.Collect();
        }
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.Configure((IApplicationBuilder app) =>
                {
                    app.UseRouting();

                    app.UseEndpoints(endpoints =>
                    {
                        var lifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();

                        endpoints.MapGet("/stop", async context =>
                        {
                            lifetime.StopApplication();
                            await context.Response.WriteAsync("Hello World!");
                        });
                    });
                });
            });
}

Further technical details

  • ASP.NET Core version: 5.0.0
  • Include the output of dotnet --info
.NET SDK (reflecting any global.json):
 Version:   5.0.201
 Commit:    a09bd5c86c

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19043
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.201\

Host (useful for support):
  Version: 5.0.4
  Commit:  f27d337295

.NET SDKs installed:
  2.1.509 [C:\Program Files\dotnet\sdk]
  2.1.511 [C:\Program Files\dotnet\sdk]
  3.0.101 [C:\Program Files\dotnet\sdk]
  3.1.201 [C:\Program Files\dotnet\sdk]
  5.0.100 [C:\Program Files\dotnet\sdk]
  5.0.103 [C:\Program Files\dotnet\sdk]
  5.0.201 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  • The IDE (VS / VS Code/ VS4Mac) you’re running on, and its version VS 2019 (16.8.6)

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 23 (17 by maintainers)

Most upvoted comments

No thank you for finding that bug. It’s been there for ages 🤣

Yea, we have a leak here. I’m gonna fix it 😄.

I’d be surprised if IIS in-proc could be restarted like this, it’s too tangled up in the IIS state.

I’ll take a look

Maybe related: https://github.com/dotnet/aspnetcore/issues/3564. Unfortunately I can’t find where that issue ended up.

It sounds like you ran hundreds of iterations but the only thing with hundreds of instances in the Count column is RuntimeType.

There’s a known issue where ASP.NET Core doesn’t clean up MemoryPoolSlabs https://github.com/dotnet/aspnetcore/issues/27394 but your issue I’m guessing is because you’re holding onto references of the host on the stack in Main.

How many host instances are hanging around? Are any of them rooted? Disposing the host should clean up the entire memory pool leaving them memory to be reclaimed by the GC (eventually). Beyond that, there’s more advanced analysis you can do to figure out why memory is being held onto (getting a memory dump after a gc and using dotnet dump analyze to look at the heap).