'Output large Json object and System.OutOfMemoryException

I need to send response of binary data(file) as JSON response in ASP.NET(IIS7) application and I use JsonObject object from System.Net.Json(comes with .NET 4.x) for that. Unfortunately sometimes I get out of memory exception.

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
   at System.Text.StringBuilder.ToString()
   at System.Net.Json.JsonUtility.EscapeString(String text)
   at System.Net.Json.JsonStringValue.WriteTo(TextWriter writer)
   at System.Net.Json.JsonCollection.WriteTo(TextWriter writer)
   at System.Net.Json.JsonObject.ToString()
   at System.Web.HttpWriter.Write(Object obj)
   at WebClient.Processor.FillResponse(HttpContext context, Object entity, OutputTypes outputType)
   at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid4[T0,T1,T2,T3](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
   at WebClient.Processor.ProcessRequest(HttpContext context)

The code

JsonUtility.GenerateIndentedJsonText = true;
var jsonObject = new JsonObjectCollection();
context.Response.ContentType = JsonUtility.MimeType;
var jsonStringValue = new JsonStringValue("File", Convert.ToBase64String(binaryData, 0, binaryData.Length));
jsonObject.Add(jsonStringValue);
context.Response.Write(jsonObject);

Any suggestions how to solve the problem?



Solution 1:[1]

Check the the memory limits and recycling time setting of the application pool in IIS.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Donal