.NET框架中的HTTP客户端中的内存泄漏4.5
我有一个使用.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();
}
}
}
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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论