My API is a .Net Core 3.1 and I have an object that can be very large depending on the query filters. The overall time is very fast (about 10 seconds for a 100k rows), but when I send it back to the client, it takes +- 2 minutes.
To return the object, I just return with the Ok method like below:
return Ok(object);
If I try to serialize this object and send the JSON as content, it's fast but I start to get out of memory exception with multiple calls because the memory is not released.
var json = JsonConvert.SerializeObject(object);
return Content(json);
I'm using Newtonsoft.Json, but I have tried the new System.Text.Json too.
Is there any other approach to work with large objects and serialization, or somehow work with the Garbage Collector to release the allocated memory?
GCSettings.LargeObjectHeapCompactionMode, see Why Large Object Heap and why do we care?.