0

I have a method ExecuteResult, which is throwing a System.OutOfMemoryException at the line Response.Write(sw.ToString()). This is happening because the StringWriter object is too large in memory for the ToString; it fills up the memory.

I've been looking around for a solution but can't seem to find a simple clean solution to the problem. Any ideas would be greatly appreciated.

Code:

public class JsonNetResult : JsonResult
{
    public JsonNetResult()
    {
        Settings = new JsonSerializerSettings
        {
            ReferenceLoopHandling = ReferenceLoopHandling.Error
        };
    }

    public JsonSerializerSettings Settings { get; private set; }

    public override void ExecuteResult(ControllerContext context)
    {
        if (this.Data != null)
        {
            if (context == null)
                throw new ArgumentNullException("context");
            if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
                throw new InvalidOperationException("JSON GET is not allowed");

            HttpResponseBase response = context.HttpContext.Response;
            response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;

            if (this.ContentEncoding != null)
                response.ContentEncoding = this.ContentEncoding;


            var scriptSerializer = JsonSerializer.Create(this.Settings);

            using (var sw = new StringWriter())
            {
                    scriptSerializer.Serialize(sw, this.Data);
                    //outofmemory exception is happening here
                    response.Write(sw.ToString());
            }
        }
    }
}
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
mgmedick
  • 686
  • 7
  • 23

1 Answers1

1

I think the problem is you are buffering all of the JSON into a StringWriter and then trying to write it out in one big chunk instead of streaming it out to the response.

Try replacing this code:

using (var sw = new StringWriter())
{
    scriptSerializer.Serialize(sw, this.Data);
    //outofmemory exception is happening here
    response.Write(sw.ToString());
}

With this:

using (StreamWriter sw = new StreamWriter(response.OutputStream, ContentEncoding))
using (JsonTextWriter jtw = new JsonTextWriter(sw))
{
    scriptSerializer.Serialize(jtw, this.Data);
}
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • That appears to have resolved the server side outofmemory exception thanks! Now I need to find why its throwing a Json out of memory error. – mgmedick Nov 09 '15 at 03:11
  • Error is this: 0x8007000e - JavaScript runtime error: Not enough storage is available to complete this operation. Strangely it works sometimes the first time on the page. Just mentioning this because you might have a quick idea whats happening there as well. Either way you solved my server side issue so marking you as the answer. – mgmedick Nov 09 '15 at 03:16
  • Sounds like a different, but related issue. I would recommend opening a new question for that, which shows your client side code and gives an indication of how much data you are trying to send to it. – Brian Rogers Nov 09 '15 at 05:19
  • Yea using chrome seems to resolve that, so I'll just investigate that separately, thanks again. – mgmedick Nov 09 '15 at 05:22
  • No problem; glad I was able to help. – Brian Rogers Nov 09 '15 at 05:23