Memory leak in HttpClient using ASP.NET Core
I encounter a memory leak problem when using HttpClient to access an HTTPS URL. Code running on ASP.NET Core + Linux server(AWS AMI).
After about 1000,000 requests, RAM occupation (RES in top) will grows to about 1GB and not release after all requests are done. If I send more requests, ram will eventually occupy all avaliable system ram and stop responding.
Testing environment: AWS c4.xlarge , Linux AMI, Dotnetcore 2.0.6 Runtime Testing method: ab -n 1000000 -c 100 http://127.0.0.1:5000/
Testing code:
static HttpClient client = new HttpClient();
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.Run(async (context) =>
{
string content = "";
using (var response = await client.GetAsync("https://some.site.com/hello")) // Only leak with HTTPS URLS
{
content = await response.Content.ReadAsStringAsync();
}
await context.Response.WriteAsync(content);
});
}
If use a HttpClient pool with some fixed number HttpClient that HttpClient only request https one at a time, the RAM will be fine(no leakage found).
Am I using HttpClient wrong? static instance vs pooled httpclient?
@lenew a memory profile would tell you what objects are leaking (or at least tell you what's taking up that 1 GB of memory). It's possible you're just allocating too many giant strings that all end up on the large object heap. How big are your responses?
Response is just a "hello" string, not large at all. I'v tried to dig into this problem with llvm dump tool, found nothing at end. This problem only occurs when using HTTPS urls, HTTP protocol works fine.
Sounds like it could be a leak in CurlHandler's SslProvider? Which means it could also be in openssl or libcurl.
I wonder if there's a way to switch to the ManagedHandler/SocketsHttpHandler in 2.0.6? @stephentoub?
@lenew Are you able to update to 2.1-preview1? If that doesn't change things, it would give you an opportunity to test the managed handler by setting COMPlus_UseManagedHttpClientHandler=true
@khellang there is no easy way to switch to SocketsHttpHandler in 2.0.x. Using 2.1 preview 1 / daily build is best option.
@lenew if you run the same workload on HttpClient from console application, do you still see the same problem?
If you can post full app @lenew I'll try to reproduce it and take a look. High usage does not necessarily mean it is leaking.
@khellang 2.1-preview have problem too 😞 @wfurt I'll post a test app later.
By now i find that this problem is relate to MaxConnectionsPerServer, if limit it to a 100, memory grow speed is slower than with it's default setting(int.MaxValue). I'll do more test on different system to see if it is relate to libcurl version or OS version.
2.1-preview have problem too
Is this using Preview 1? If so, does it still show a problem if you first do:
export COMPlus_UseManagedHttpClientHandler=true
?
@stephentoub @khellang I forgot to set COMPlus_UseManagedHttpClientHandler=true After set it to true and 2000000 requests, no leakage anymore. Memory occupation is stable at 450~500M. Tests on different Os and libcurl:
- Ubuntu 14.04LTS + libcurl4-gnutls + dotnetcore 2.0.6, no leakge, 429M
- AMI Linux + libcurl-7.59.0-mbedlts, no leakage, 780M
- AMI Linux + libcurl-7.59.0-openssl, no leakage, 780M
So, this problem is relate libcurl version. I'll replace system default libcurl with latest one to fix this problem. ManagedHttpClientHandler have a very good performance!
Is there a way to update libcurl in docker in the .net core images?
Use image based on newer OS. .Net really does not care that much. 2.0 is out of support and 2.1 has socket handler enabled by default.