CookieContainer memory leak

290 views Asked by At

I'm seeing a small memory leak coupled with gradual CPU usage increase over time in an app that is using static HttpClient to regularly hit distinct urls that return set-cookie headers. These issues are solved by setting UseCookies = false on HttpClientHandler. That led me to investigate CookieContainer and I found that it maintains a hashtable (m_domaintable) of domains and for each domain it maintains a sorted list of paths (m_list). So even though cookies are correctly removed based on Capacity and PerDomainCapacity, memory and cpu usage grow over time as these collections grow with each new domain/path that is hit. There are some examples below that show the memory leak.

First example: add a cookie for many distinct domains

var cookieContainer = new CookieContainer();
for (int i = 0; i < 10000; i++)
{
    cookieContainer.Add(new Cookie("name1", "value1", "/", $"test{i}.com"));
}

Second example: add a cookie for the same domain with different paths

var cookieContainer = new CookieContainer();
for (int i = 0; i < 10000; i++)
{
    cookieContainer.Add(new Cookie("name1", "value1", $"/{i}", "test.com"));
}

Is there something I'm missing here in my usage of HttpClient/HttpClientHandler/CookieContainer? The documentation states that HttpClient should re-used throughout the life of an application but that means that CookieContainer will also be re-used and these collections will continue to grow.

1

There are 1 answers

0
Will On BEST ANSWER

There is a CookieContainer bug that is causing this issue: https://github.com/dotnet/corefx/issues/33712