aspnetcore: Health Check Causes Memory Leak in .NET 8

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

In a dot net core 8 application, health checks executed on a regular interval eventually leak all available memory, causing the container to crash.

Expected Behavior

Health check should not leak memory. Memory usage should remain relatively consistent or at least be reclaimed by garbage collection.

Steps To Reproduce

Reproduced with the following system configuration:

  • Windows 11 build 22631.3155
  • SDK: 8.0.102
  • Runtime: 8.0.2
  1. Setup new dot net core web API application using .NET 8 as the target framework
  2. Modify the auto generated Program.cs to include the following block of code
        builder.Services.AddHealthChecks();

        var app = builder.Build();
        app.MapHealthChecks("/hc", new HealthCheckOptions
        {
            AllowCachingResponses = false,
        });

From there, run the program with a memory profiler (I used DotMemory version 2023.3.3). Notice that each time /hc is called, the health check leaks ~100KB of RAM.

Now, modify the *.csproj file to change the target framework to dot net 6;

<TargetFramework>net6.0</TargetFramework>

Note:

  • You will need to remove the OpenAPI reference in the *.csproj
  • You will also need to remove the .WithOpenApi(); extension in Program.cs

Now repeat the profiling exercise. Notice that, although the memory is not 100% constant, it doesn’t really exhibit “leaking” behavior.

Please advise as to how to proceed. If you setup a docker container to poll the service at ~10 second intervals, you’ll end up with hundreds of MBs of RAM leaked over a few hour timeframe.

Severity: SEVERE; risk of production crash. Framework is effectively not usable.

Exceptions (if any)

Memory should not leak during basic health check.

.NET Version

8.0.102

Anything else?

Similar to issue reported in the runtime itself, https://github.com/dotnet/runtime/issues/99304

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 33 (8 by maintainers)

Most upvoted comments

I need to correct my previous assumption that disabling health checks resolved our issue; the services still ran out of RAM. As @debracey mentioned, there could be multiple reasons causing the leak. In my dump, System.Collections.Generic.Dictionary<System.String, Serilog.Events.LogEventPropertyValue>+Entry[] is one of the largest consumers of memory.

@debracey, we are facing the same issue. Unfortunately, it was my oversight in missing the memory leak problem when upgrading the services to .NET 8 in production. Now, we find ourselves in a situation where our HPAs are scaling the pods excessively, much like wildfire. Manual intervention is required because we do not wish to change our deployment configuration.

image

My particular issue was resolved using the approach I described above. The package had an infinite queue limit when Loki was unreachable, which filled up the memory until an OutOfMemoryException was thrown.

Initially, it was a combination of both health checks and the leaking package. However, with ASP.NET 8.0.4 and SDK 8.0.203, along with the proper adjustments to the logger configuration, all my issues were resolved.

@debracey, if your error still persists, I recommend getting a dump of the process and inspecting the highest consumers. In my case, it was System.Collections.Generic.Dictionary<System.String, Serilog.Events.LogEventPropertyValue>+Entry[] which pinpointed the issue for me.

After weeks of debugging and head-cracking, I can finally utilize those C#12 features 🚀. Thank you for the help, everyone; you do a magnificent job!

Yes. We also see the same. I thought the output was “No roots found” – but I’m not at my work computer so presumably @andreiZi has the correct output.

Hey @danmoseley
Yes, i consistently receive the following output: Found 0 unique roots.

Here you can see the service I posted about 1 hour ago. Just as @debracey described, it stabilizes the memory consumption for a while, but now we can clearly see that it starts to increase again. image

Here are the last relevant symbols from the output of dumpheap -s:

7fca8bca63b8     312    455,082 System.Char[]
7fca906bdb58  22,620    723,840 Serilog.Events.StructureValue
7fca8e28b438  23,462    750,784 Serilog.Events.LogEventProperty
7fca8e53bf28  22,623    904,920 Serilog.Sinks.Grafana.Loki.Models.LokiLogEvent
7fca8ef08948  23,187    932,320 Serilog.Events.LogEventProperty[]
7fca906b7718  22,623  1,809,840 Serilog.Events.LogEvent
7fca906d8bb0  22,623  1,809,840 System.Collections.Generic.Dictionary<System.String, Serilog.Events.LogEventPropertyValue>
7fca8bbd89e0  23,440  1,967,548 System.Int32[]
7fca9024e6b0 191,221  4,589,304 Serilog.Events.ScalarValue
7fca906d9a30  22,905  8,306,688 System.Collections.Generic.Dictionary<System.String, Serilog.Events.LogEventPropertyValue>+Entry[]
7fca8bbdd7c8 134,245 26,716,442 System.String
7fca8c769800   4,299 50,249,261 System.Byte[]
560b529c1790  13,560 75,716,040 Free

@GimmeDaKitty, @Kiechlus I Went back and looked at my notes on this now that I am back in office. In the test described above I wasn’t using a container at all, I was just running the service directly on windows out of my IDE (Rider 2023.3.3).

I upgraded my SDK to 8.0.203 and the issue seems to be resolved. I no longer see memory increasing (in the unmanaged memory section) with each call to HC.

I’ll reach out to my infrastructure team to see what SDK our build pipelines use. I’ll report back again once I can retest with an actual productionalized service.