3

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?

9
  • Does this answer your question? Serializing an object using Json.Net causes Out of Memory exception Commented Jun 19, 2020 at 13:38
  • @BrunoXavier That would avoid the out of memory but the time to read and write a File will not help. I have already tried this approach but it takes more time than returning Ok(object) Commented Jun 19, 2020 at 13:49
  • and I believe paging wouldn't be a viable solution? Commented Jun 19, 2020 at 13:56
  • 1) Have you tried profiling the +- 2 minutes to see where the problem is? 2) You might try setting GCSettings.LargeObjectHeapCompactionMode, see Why Large Object Heap and why do we care?. Commented Jun 19, 2020 at 14:04
  • @BrunoXavier Yes, paging is a good approach, but for this specific case, I need to return a large Json (or other format maybe) Commented Jun 19, 2020 at 14:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.