I have an ASP.NET Core 8.0 application where I want to move some controllers into a module (separate project). To accomplish this I had to update the startup from:
services.AddControllersWithViews()
.AddRazorRuntimeCompilation();
to:
var assembly = typeof(HomeController).Assembly;
services.AddControllersWithViews()
.AddApplicationPart(assembly)
.AddRazorRuntimeCompilation(options =>
{
options.FileProviders.Add(new EmbeddedFileProvider(assembly));
});
This works fine when I start the application (it starts and all endpoints/views are available as before), but I have a problem when running the tests suite.
The tests run with xUnit and use a base class to setup an instance of the service and then uses API calls and a browser instance to call the endpoints on the service. For each test a new instance of the service is initialized. This worked fine before the change with a memory usage of around 800MB, but after the change the memory usage went up to 13GB. It is a testsuite of 200 tests and during the run gradually the memory increases. So it looks like, after the change, some memory is not released after each test.
Some things I have tried:
- Made assembly a static field
- Tried all sorts of different orders of calling these methods
- Tried adding dispose methods on the service host
- Tried to profile memory usage, this doesn't teach me much, but in the memory snapshot I see a lot of "Razor Compilation" stuff coming back. But it doesn't give a clear indicator. I see there are a lot of "instances" still active in memory:
I am very much at a loss about how this change could cause a memory leak, so any leads or ideas of things to try would be much appreciated.