简体   繁体   中英

.Net 5 IHost FileSystemWatcher memory leak

I'm currently working on a plugin-like system that requires loading in assemblies at runtime.

The root app will run as a windows service, and will create an IHost for each 'plugin' so that each one can have its own DI container.

I started doing stress tests on it to see if there was any memory leaks when stopping/starting the 'plugins'. Stopping/Starting involves stopping the IHost and disposing of it, then starting a new one. I found that there was a small memory leak somewhere. It seemed like I was gaining 10-50mb for every ~1000 restarts.

That's not terrible, but it made me think I wasn't disposing of something correctly. I ended up finding that just starting/stopping a default IHost seemed to cause a similar issue (Though to a lesser degree). Below is a simple console app to show pretty much what I'm doing, and you can see it just slowly but consistently gains memory usage. I can see the GC is being run, so I don't think that it would ever get cleaned up.

class Program
{
    private static async Task Main(string[] args)
    {
        while (true)
        {
            var host = Host.CreateDefaultBuilder().Build();
            await host.StartAsync();

            await host.StopAsync();
            host.Dispose();
        }
    }
}

And here is the memory snapshot. You can see there are a few things that are consistently building up. The highlighted items count seems to marry up with the number of looks that have occured.

So to me, it seems like there is a file watcher being created at some point in the IHost that isn't properly being disposed. I'm guessing it might be for one of the configuration files?

内存快照

I added WriteLine to track the number of loops that have occured and ran it for a couple minutes. The application starts at ~10-20mb of memory being used, but after 17000 loops it had gotten up to 200mb+. This is with the GC.Collect and GC.WaitForPendingFinalizers being called at the end of each loop.

循环计数的内存使用情况

My question is whether this is an issue or not. Am I diagnosing this as a memory leak when it isn't?

It probably wont effect me because I don't expect thousands of restarts, but is it worth creating an issue on the github if this is actually a leak?

EDIT:

I think I've confirmed it's a memory leak. If I remove all the configuration sources by adding the following before .Build() the memory issue goes away.

ConfigureAppConfiguration(config =>
{
    config.Sources.Clear();
})

Is this something I should add as an issue at https://github.com/dotnet/aspnetcore/issues ?

As I said in my edit, it looked to be due to the configuration setup.

I made an issue on the ASP Net Core Github , but it looked like someone else had already reported it a couple days prior.

Seems like David Fowler has already added pull requests to the.Net 6 preview so it should be fixed for.Net 6.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM