无法在Asp.Net Core API中释放内存 [英] Memory not getting freed up in Asp.Net core api

查看:503
本文介绍了无法在Asp.Net Core API中释放内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,无法在.NET Core Web API中释放内存.我要做的只是从静态类返回一个字符串.

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";
    }
}

客户:

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);
    }
}

我尝试了直接从action方法返回字符串而没有静态类.我还尝试以同步方式而不是异步方式从客户端调用API.这些都没有任何改变.

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.

此屏幕截图显示了一次调用GC的地方,但无助于清理内存.

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

以下是升级到2.0并禁用遥测信息后的诊断信息:

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

推荐答案

似乎即使没有使用Application Insights也在收集数据,因此您会看到内存增加.一段时间后,这些对象由GC收集.

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.

默认情况下,Visual Studio会打开应用程序见解.有两个选项可以关闭应用程序见解":

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

  1. TelemetryConfiguration.Active.DisableTelemetry = true;添加到ConfigureServices,您应该会看到性能改进.
  2. 在launchSettings.json中添加ASPNETCORE_preventHostingStartup键:
  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.

这篇关于无法在Asp.Net Core API中释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆