0

How should handle System.OutOfMemoryException exception while parsing a large string which is coming as an API Response.

I am getting this exception while parsing a large string

Exception of type 'System.OutOfMemoryException' was thrown. System.OutOfMemoryException OutOfMemoryException System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
   at Newtonsoft.Json.Linq.JToken.SetLineInfo(IJsonLineInfo lineInfo, JsonLoadSettings settings)
   at Newtonsoft.Json.Linq.JContainer.ReadContentFrom(JsonReader r, JsonLoadSettings settings)

This is the simple code that I am running.

var jObject= JObject.Parse(responseString);
6
  • The exception seems to be rather clear. What is your question? Commented Apr 14, 2022 at 11:14
  • How should I parse a large string that could cause this exception? Commented Apr 14, 2022 at 11:15
  • what version of Newtonsoft are you using? I've been having problems with 13 (crashing the whole server). So we went back to 12.0.3 and everything is ok again. see this issue Commented Apr 14, 2022 at 11:17
  • 2
    is this responseString originally a Stream? if yes, you might be interested to parse the stream directly, see docs. Commented Apr 14, 2022 at 11:17
  • 1
    Does this answer your question? Out of memory exception while loading large json file from disk Commented Apr 14, 2022 at 11:19

1 Answer 1

0

You should use the StreamRreader class to parse some of the message at a time.

Sign up to request clarification or add additional context in comments.

2 Comments

this could indeed help: please expand your answer with examples
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.