HTTP Client Deserialize JSON result in OOM (Out Of Memory) Exception
.NET CORE 3.1 Application
We have an application that will invoke web API (pull data from database) and return the data set to client(s) as above diagram. The system all works fine but some day went to OOM (Out Of Memory) issue suddenly:
After investigated the main reason was caused by the data volume from database return to API that increased from 100K to 360K records per query (with same criteria). The totals amount of data will return (from API) as JSON string and desterilized into List<Object> that throw OOM exception inside of application.
We have do some research for trying allocate less memory space as possible under the current scenario. The article suggested us to response stream instead of string would be cue direction. So we refactored our POST method as PostReturnStream to see would be different for memory usage.
We could using IHttpClientFactory for avoiding repeating initialize/deinitialize HttpClient instance(s) of each request.
Comparing with PostReturnString VS PostReturnStream with same volume of huge data (280K) that retrieved back from API. Indeed the PostReturnStream could save more memory usage than PostReturnString.
📌PostReturnString → API → TO List<Object> (364MB)
📌PostReturnStream → API → TO List<Object> (272MB)
We might considerate more carefully while application(s) interactive with API(s) for the condition:
🎯 Setup threshold (maybe page count) for each return data set per API invoke and try to batch processing if the total amount is exceed the threshold.🎯 Check if your application run under x86 mode. Compile to x64 will benefit from possibility having more space available in RAM.🎯 Consider use tool like Polly to handle errors(timeout, retries...) between application and API.