HTTP Client Deserialize JSON result in OOM (Out Of Memory) Exception

Case study

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.

Experiment

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.

280K Items — PostReturnString
280K Items — PostReturnStream

📌PostReturnString → API → TO List<Object> (364MB)

📌PostReturnStream → API → TO List<Object> (272MB)

Overall

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.

Reference

Coding for fun

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Push object in array of react hook

How to start working with Styled Components in React

React component patterns to use based on need (Part 3)

Ionic 5 and above: SSL Pinning for iOS

How to make a twitch bot (twytch)

Data Structures in JavaScript

Classes in Ruby vs JavaScript

Angular Dynamic Forms using FormsArray

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
LAI TOCA

LAI TOCA

Coding for fun

More from Medium

Read request body multiple times in ASP.NET Core

Add MVC Controller With Views using Entity Framework in NET 6

Add AutoMapper to .Net 6

Custom Configuration Providers in ASP.NET Core