0

I have a mobile app that makes a call to a web service using an HttpWebRequest, and then parses it into a JArray. I am running into a memory issue when the JSON string is long (~20MB), where my iPad is receiving memory warnings and ultimately crashes.

When I exclude the JArray.Parse step, I do not see these memory warnings.

Are there any alternatives to building my JArray that would be more efficient memory-wise?

string longJsonStringFromWebService = "";

HttpWebRequest request = ...
using(WebResponse response = await request.GetResponseAsync())
{
    using (StreamReader streamReader = new StreamReader(response.GetResponseStream())
    {
        longJsonStringFromWebService = reader.ReadToEnd();
    }
}

...

JArray jsonArray = JArray.Parse(longJsonStringFromWebService);
jkh
  • 3,618
  • 8
  • 38
  • 66
  • You're holding all data in memory twice after executing that last line. Instead of doing a `ReadToEnd()`, you could read item per item and append the last-read item to the array instead, that would require a lot less memory. – Roy Dictus Sep 15 '15 at 14:02
  • @RoyDictus These long JSON strings contain attachment filestream data. They typically have a low number of keys with very long values, so I'm not sure that reading by item would have too big of an impact, unless there is a way to do it without duplicating the individual field during the parse. – jkh Sep 15 '15 at 14:49
  • See also: [Deserialize json array stream one item at a time](http://stackoverflow.com/q/20374083/10263) – Brian Rogers Sep 15 '15 at 16:24

0 Answers0