Need help analyzing ASP.Net Core 3.1 memory dump

1.5k Views Asked by At

I'm prepared to pay for help on this!!

My ASP.Net Core 3.1 application starts at around 450MB, and gradually runs up to around 4.5GB (and I suspect it would grow even more if more memory were available to it).

I have taken memory dumps at various stages, and analyzing them with dotMemory seems to show that JsonSerialiserOptions is a prime suspect:

largest retained size

Drilling to JsonSerializerOptions shows the 3 instances. Two have negligible numbers, and if I further drill into the one of the 3 with the highest memory use, it's Key Retention Path shows this: Key Retention Paths

And this is where I need help. I really don't know what to make of these retention paths. I'm expecting that if the problem is in my code, I should find some class of my app at the bottom of a retention path? I need help trying to figure out from these retention paths where in my code the problem may be.

2

There are 2 best solutions below

2
Shawn de Wet On BEST ANSWER

It turns out the problem was due to a bug in which a flexible, run-time-defined database query was retuning instances of type Object, instead of instances of type dynamic. The JsonSerializer correctly does NOT cache dynamic types, but it DOES cache Object types.

So each time the query ran, the JsonSerializer was caching the Object structure. The fix was to ensure the the database query returned dynamic types instead of Object types.

1
michal krzych On

I suspect your problem relates to the reload change token. A memory leak reported way back in .NET 2.1 - for more details follow: https://github.com/dotnet/aspnetcore/issues/6102

but

if you're targeting a newer version of the .Net I'd recommend inspecting your ConfigureServices() method (Startup.cs) and eliminating misconfigured services lifetimes and/or captive dependencies.