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