【问题标题】:.Net Core - Docker Linux memory usage keeps increasing.Net Core - Docker Linux 内存使用量不断增加
【发布时间】:2022-08-17 22:20:56
【问题描述】:

我现在有点沮丧,我的代码出了什么问题,我希望你们能帮助我,所以这里是我尝试过的事情。

所以我尝试将 HttpClient 设为静态,并尝试使用 IHttpClientFactory.CreateClient(),甚至将其添加到我的 .csproj

<ServerGarbageCollection>false</ServerGarbageCollection>

这是我一直在做的示例代码

public class TestController : BaseController
{
    private static HttpClient _httpClient = new();

    public TestController()
    {
    }

    [HttpGet(\"bills\")]
    public async Task<IActionResult> GetBillsPresentment([FromQuery] GetBillPresentmentQuery query)
    {
        if (!query.AccountNumber.Contains(\"-\"))
            query.AccountNumber = FormatAccountNumber(query.AccountNumber);

        var billDetails = await GetBillDetail(query.AccountNumber);

        if (billDetails == null)
            throw new ProviderProcessException(ProviderErrorCode.INVALID_ACCOUNT_NUMBER);

        return Ok(new BillPresentmentVm
        {
            User = new CustomerDto
            {
                CustomerName = billDetails.Name
            },
            Billing = new BillingDto
            {
                AccountNumber = query.AccountNumber,
                DueDate = DateTime.Parse(billDetails.LastReadDate).AddMonths(1),
                Outstanding = !string.IsNullOrEmpty(billDetails.Arrears) ? decimal.Parse(billDetails.Arrears) : null
            }
        });
    }

    private async Task<ResponseModel> GetBillDetail(string accountNumber)
    {
        try
        {
            var payload = new { accno = accountNumber };

            string json = JsonConvert.SerializeObject(payload);

            var buffer = System.Text.Encoding.UTF8.GetBytes(json);
            using var byteContent = new ByteArrayContent(buffer);
            byteContent.Headers.ContentType = new MediaTypeHeaderValue(\"application/json\");

            var response = await _httpClient.PostAsync(\"https://test.com\", byteContent);

            if (!response.IsSuccessStatusCode)
                throw new ProviderProcessException(ProviderErrorCode.BILLING_CYCLE_UNAVAILABLE);

            var result = await response.Content.ReadAsStringAsync();

            if (result == \"Accno not found!\") return null;

            var data = JsonConvert.DeserializeObject<ResponseModel>(result);
            return data;
        }
        catch (Exception)
        {
            throw new ProviderProcessException(ProviderErrorCode.BILLING_CYCLE_UNAVAILABLE);
        }
    }

    private static string FormatAccountNumber(string accountNumber)
    {
        return string.Format(\"{0:#######-########}\", Convert.ToInt64(accountNumber));
    }
}

这是 docker 内存使用情况

请求后内存使用量不断增加。有人能解释一下为什么它没有减少吗?

非常感谢您提前

  • 您应该阅读有关该主题的 Microsoft 文章:HTTP with .NET 因为在 ASP.NET 中有一个静态的 HttpClient 实例?馊主意。
  • @HereticMonkey 就像我上面所说的那样,我也尝试了 HttpClientFactory 仍然是相同的结果伙伴
  • 不可能从几行代码和屏幕截图中调试内存泄漏。您必须努力使用分析器来查找泄漏。

标签: c# docker .net-core


【解决方案1】:
猜你喜欢
  • 2017-09-17
  • 2011-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-02
  • 2018-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多