C#C
C#3y ago
Daltz333

✅ Optimizing `HTTPClient.GetAsync()` and large strings

I am writing a client application that consumes a web API at a near real-time rate (100ms). I am using httpClient.GetAsync() to retrieve the content and then I grab the string contents with Content.ReadAsStringAsync().

Then, I deserialize it with JsonConvert.DeserializeObject<T>() to a format I expect (in some cases (out of my control), it's impossible to directly deserialize, in which case I deserialize to dynamic to parse manually).

Because this is done every 100ms, it's creating a large number of GC (short, but frequent) creating some pretty bad GC pressure. This is on the count of ~10 ~5ms GCs/s. Unfortunately, this can dramatically slow down execution, especially since GCs block all threads, including GUI.

I assume there are strategies to mitigate and solve this, this has to be a solved problem. Unfortunately, I'm not finding reliable/updated answers in google (the most common answer is... don't use C#).
Was this page helpful?