Getting an OutOfMemoryException while serialising to JSON?

Assuming you don't have Circular References - if you can't store the whole thing in memory use a StreamWriter(JsonWriter or TextWriter) in Newtonsoft v4.0.30319

using (TextWriter writer = File.CreateText("LocalJSONFile.JSON"))
{
    var serializer = new JsonSerializer();
    serializer.Serialize(writer, myObject);
}

Use JsonWriter if you are trying to pass the string

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);

using(JsonWriter writer = new JsonTextWriter(sw))
{
  var serializer = new JsonSerializer();
  serializer.Serialize(writer, myObject);
}

It appears that you're running into Ciruclar Reference that is causing OutOfMemoryException or your objects are simply too large for your memory. Use NDepend to check this.

You might find useful getting the total size of your objects.


It's hard to tell without knowing the exact structure of the objects being serialized, but it's possible that, since the object graph is so big, there might be circular references somewhere (an object which points to an object which in turn points to the first object), creating an infinite loop of serialization.

EDIT :

You may use a tool, like NDepend, to find out where the circular references are. Give the trial version a try.


Comments

  1. Taylor

    • 2021/4/25

    Assuming you don't have Circular References - if you can't store the whole thing in memory use a StreamWriter(JsonWriter or TextWriter) in 

  2. Roland

    • 2019/8/29

    Assuming you don't have Circular References - if you can't store the whole thing in memory use a StreamWriter (JsonWriter or TextWriter) in Newtonsoft v4.0.30319. It appears that you're running into Ciruclar Reference that is causing OutOfMemoryException or your objects are simply too large for your memory.

  3. Collins

    • 2017/7/7

    Hi , I am getting out of memory exception while serializing large data using NewtonSoft json serializer. Below is the exception i am 

  4. Francisco

    • 2015/3/17

    I am getting out of memory exception while serializing large data using NewtonSoft json serializer. Below is the exception i am getting:-System.OutOfMemoryException: OutOfMemoryException at System.String.GetStringForStringBuilder(String value, Int32 startIndex, Int32 length, Int32 capacity)

  5. Blaise

    • 2018/8/7

    In that case we get a System.StackOverflowException: 'Exception of type 'System.StackOverflowException' was thrown.' as it tries to serialize at 

  6. Boden

    • 2015/4/11

    Newtonsoft.Json - Out of memory exception while deserializing big object. I have a problem deserializing a JSON file of about 1GB. When I run the following code I get an out of memory exception: using (FileStream sr = new FileStream ("myFile.json", FileMode.Open, FileAccess.Read)) { using (StreamReader reader = new StreamReader (sr)) { using

  7. Emmitt

    • 2017/2/11

    However in very few large records I still encounter this error when /8630736/getting-an-outofmemoryexception-while-serialising-to-json.

  8. Elian

    • 2017/6/24

    Hi gariani, Thank you for posting here. According to your description, you want to solve the exception that you got when serializing. The following link could be helpful for you.

  9. Carlos

    • 2015/1/22

    I either get a hanging process taking a lot of CPU, or a StackOverflow Exception (if run from xunit). This issue seems to only affect Json.Net 

  10. Dupont

    • 2019/9/30

    if (traceJsonWriter != null) TraceWriter.Trace(TraceLevel.Verbose, "Serialized JSON: " + Environment.NewLine + traceJsonWriter.GetJson(), null); GetJson() calls StringWriter.ToString() causing the OutOfMemoryException to be thrown.

  11. Briar

    • 2018/9/10

    I am serializing , a MultiDictionary http://powercollections.codeplex.com/ to json .It has 618 elements with elements being deeply nested ,i.e. a single 

  12. Lukas

    • 2017/10/21

    Sometimes while running this process I will get an aggregate exception which shows an inner exception as out of memory. I can't determine if it is the process of reading in the string of JSON data (which is probably awfully large) or the Deserializing that is causing this issue.

  13. Erick

    • 2019/1/13

    Json package is probably one of the most essential packages in . we experienced “Out of memory exception” in a matter of minutes.

  14. Creed

    • 2019/7/3

    If you are using a large data table and you are getting out of memory issues, it may well be that the size of the JSON string is just too big for .NET - there is a limit of 2GB on any single object in .NET, and since JSON is a text-based serialization a large table could well exceed that even if the "raw" data table is considerably less than that.

  15. Donald

    • 2019/4/19

    If the JSON returned is large, we'll often get an Out Of Memory Exception. From the docs. To minimize memory usage and the number of objects 

  16. Harrison

    • 2020/6/12

    Note that if you are not using a contract resolver then a shared internal instance is automatically used when serializing and deserializing.

  17. Maximiliano

    • 2015/10/1

    Tables[0]); the Query return a large datatable and when i convert it to json by serializing System.OutOfMemoryException' was thrown. How can i 

Comments are closed.

Recent Posts