0

I am parsing local data to JSON, and then I parse JSON to XML. Parsing to JSON works fine, and I'm pretty sure that a week or two ago parsing to XML also worked fine - it wasn't in production, but I tested it and it worked. Now I get above-mentioned exception. Here's my code:

public static string ParseData(Data data)
{
    string xmlString = string.Empty;
    XmlData xmlData = new XmlData(data);

    using (MemoryStream memoryStream = new MemoryStream())
    using (StreamReader reader = new StreamReader(memoryStream))
    {
        xmlSerializer.WriteObject(memoryStream, xmlData);
        memoryStream.Position = 0;
        xmlString = reader.ReadToEnd(); //exception occurs here
    }
    return xmlString;
}

When exception occurs memoryStream.Position's value equals it's length and I still have like 200-300 mb in RAM. It's 64-bit app and 64-bit system. I checked similar questions, but in my case there is no big amounts of data(json is 1.5mb max).

UPDATED. Stack trace:

"at System.Text.StringBuilder.ToString()\r\n at System.IO.StreamReader.ReadToEnd()\r\n at Common.Util.LiveScoringXml.ParseData(Data data) in C:\Source\Repos\Latest\Common\Util\LiveScoringXml.cs:line 30"

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Jamil
  • 830
  • 2
  • 15
  • 34

1 Answers1

4

If you have a 64 bit application the most probably reason of your problem (apart from a bug in the .Net Framework) is that you are having the issue due to fragmentation of the Large Object Heap memory, any object bigger than 80k is stored there.

Check the following links:

Why Large Object Heap and why do we care?

https://blogs.msdn.microsoft.com/dotnet/2011/10/03/large-object-heap-improvements-in-net-4-5/

You are probably generating big chunks of data quite often and fragmenting the LOH until you cannot find a contiguous chunk big enough.

There are strategies to solve the issue like reusing objects instead of creating and GC'ing them.

Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207