.NET框架中的HTTP客户端中的内存泄漏4.5

发布于 2025-02-13 00:28:10 字数 1765 浏览 2 评论 0原文

我有一个使用.NET Framework 4.5构建的Windows Service应用程序。该服务在持续时间持续更长的时间时会累积内存,并且逐渐逐渐占用100-200MB。 这项服务在广义上的作用是每分钟一次打电话,然后对响应(一种民意调查)采取行动。我正在使用HTTPCLIENT的单个实例进行所有请求,在分析内存转储后看到的异常是_SSL流的存在,这些流未被处置,我相信这些对象在内部使用httpclclient库。另外,要指出的另一件事是,仅当服务无法达到API时,就会发生这种情况,当API到达时,内存就不会突然增加,_SSSLSTREAM不存在对象。在内存转储中。 在代码段中,如下所示getDataAsync每分钟都调用timerObject 有什么可能的原因是什么?

示例代码段

public class Sample //This class has a single instance in the app maintained by the Unity container
{
private HttpClient Client {get;set;}

public Sample() {
  System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; //To support both TLS1.1 and 1.2
  this.Client = new HttpClient();
}

private async Task < string > GetDataAsync(Uri completeUrl) {

    HttpRequestMessage httpRequestMessage = new HttpRequestMessage {
      Method = HttpMethod.Get,
        RequestUri = completeUrl,
    };
    HttpResponseMessage responseMessage = null;
  

      httpRequestMessage.Headers.Add("Authorization", new AuthenticationHeaderValue("Bearer", "SomeAccessToken");

        try {
          responseMessage = await Client.SendAsync(httpRequestMessage);
        } catch (Exception ex) {
          throw ex;
        }
        finally
        {
            httpRequestMessage?.Dispose();
            responseMessage?.Dispose();
        }
}
}

“在此处输入图像描述” SSL流对象的详细视图

I have a windows service application built with .Net framework 4.5. This service when kept running for a longer duration keeps on accumulating memory and slowly gradually it takes up to 100-200MB.
The role of this service on broad level is to make some GET call every minute and then act on the response(kind of polling). I am using a single instance of HttpClient for all the requests the anomaly I am seeing after analyzing memory dumps is the presence of _SSL streams that are not getting disposed of, I believe these objects are getting used internally inside HttpClient library. Also, another thing to point out is this scenario is occurring only when the service is not able to reach to the API, when API is reachable then there is no abrupt increase in memory and _SSLStream objects are not present in memory dumps.
In the code snippet as shown below GetDataAsync is called by TimerObject every minute
What could be the possible reason for the same?

Sample code snippet

public class Sample //This class has a single instance in the app maintained by the Unity container
{
private HttpClient Client {get;set;}

public Sample() {
  System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; //To support both TLS1.1 and 1.2
  this.Client = new HttpClient();
}

private async Task < string > GetDataAsync(Uri completeUrl) {

    HttpRequestMessage httpRequestMessage = new HttpRequestMessage {
      Method = HttpMethod.Get,
        RequestUri = completeUrl,
    };
    HttpResponseMessage responseMessage = null;
  

      httpRequestMessage.Headers.Add("Authorization", new AuthenticationHeaderValue("Bearer", "SomeAccessToken");

        try {
          responseMessage = await Client.SendAsync(httpRequestMessage);
        } catch (Exception ex) {
          throw ex;
        }
        finally
        {
            httpRequestMessage?.Dispose();
            responseMessage?.Dispose();
        }
}
}

enter image description here
Detailed view of SSL stream object
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文