简体   繁体   中英

Memory leak when calling unmanaged code from net core

I have a project using Microsoft.SqlServer.Types that leaks memory and slows down when retargeting from net472 to netcoreapp3.1.

I'm aware that netcoreapp3.1 don't necessarily can run netfx dlls, but I tried just to see if it would work.

I'm observing two things:

  • The memory usage is slowly but steadily increasing.
    • netcoreapp3.1 reached 500mb (and continued to increase), where the net472 is stable around 15mb
  • Calls to ShortestLineTo seems to get slower and slower as the programs runs for longer time.
    • net472 is quite stable around at 2500 ms, while netcoreapp3.1 starts out at comparable times, but after some time has increased to 4500ms.
internal static class Program
{
    private static void Main()
    {
        var point = SqlGeography.Point(0, 0);
        var sw = Stopwatch.StartNew();

        while (true)
        {
            for (int i = 0; i < 100_000; i++)
            {
                point.ShortestLineTo(point);
            }

            Console.WriteLine(sw.ElapsedMilliseconds);
            sw.Restart();
        }
    }
}

https://github.com/dotnet/runtime/issues/353 mentions a memory leak for Marshal.GetFunctionPointerForDelegate but I'm unsure whether this is the same case.

Running Concurrency Visualizer gave this significant trace. 并发可视化工具转储

I've created a smaller example that extracts the relevant parts from the nupkg and calls the unmanaged dll. https://gofile.io/?c=hfAKwe

Edit 2020-03-20:

Tried running the same program with the newly release .Net 5.0 preview1 and now both memory and cpu usage has stabilized.

该修复程序已向后移植到 .net core 3.1.8 https://github.com/dotnet/coreclr/pull/28074

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