Memory not getting freed up in Asp.Net core api

三世轮回 提交于 2019-12-24 01:24:32

问题


I have an issue where the memory is not getting freed up in my .NET Core Web API. All I am doing is simply returning a string from a static class.

API:

[HttpGet]
public string Get()
{
    return A.get();
}

public static class A
{
    public static string get()
    {
        return "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    }
}

Client:

HttpClient hc = new HttpClient();
for(int i=0;i<100000;i++)
{
    //var v = hc.GetAsync("http://localhost:52425/api/values").result;
    hc.GetStringAsync("http://localhost:52425/api/values");
    if(i%1000 == 0)
    {
        Thread.Sleep(10000);
        Console.WriteLine("Sleeping-----------" + i);
    }
}

I have tried returning the string directly from the action method without the static class. I have also tried to call the API from the client in synchronous fashion rather than async. None of these made any difference.

This screenshot shows that where GC is called once but does not help clear up the memory.

Here are the diagnostics after upgrading to 2.0 and disabling telemetry info:


回答1:


It seems that Application Insights is collecting data even when you are not using it and thus you see memory increase. After some time those objects are collected by GC.

Application insights are turned on by default by Visual Studio. There are two options to turn off Application Insights:

  1. Add TelemetryConfiguration.Active.DisableTelemetry = true; to ConfigureServices and you should see performance improvements.
  2. In launchSettings.json add ASPNETCORE_preventHostingStartup key:
"environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development",
    "ASPNETCORE_preventHostingStartup": "True"
  }

This one could potentionaly tun off some other stuff. Read more about it here.



来源:https://stackoverflow.com/questions/46671597/memory-not-getting-freed-up-in-asp-net-core-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!