Asked by:
Out of memory exception c#

Question
-
User1247222288 posted
Hi,
I am getting below exception while serializing/de-serializing an object in c# using Newtonsoft.json
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.OutOfMemoryException
at Microsoft.IO.RecyclableMemoryStreamManager.GetBlock()
at Microsoft.IO.RecyclableMemoryStream.EnsureCapacity(Int32)
at Microsoft.IO.RecyclableMemoryStream.Write(Byte[], Int32, Int32)
at Cassandra.Connection.ReadParse(Byte[], Int32)
at Cassandra.Connection.ReadHandler(Byte[], Int32)
at Cassandra.TcpSocket.OnReceiveCompleted(System.Object, System.Net.Sockets.SocketAsyncEventArgs)
at System.Net.Sockets.SocketAsyncEventArgs.OnCompleted(System.Net.Sockets.SocketAsyncEventArgs)
at System.Net.Sockets.SocketAsyncEventArgs.ExecutionCallback(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationSuccess(System.Net.Sockets.SocketError, Int32, System.Net.Sockets.SocketFlags)
at System.Net.Sockets.SocketAsyncEventArgs.CompletionPortCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)The above exception is from event viewer application logs.
Exception log from application:
"Message": "Exception of type 'System.OutOfMemoryException' was thrown.",
"StackTrace": " at System.Text.StringBuilder.ToString()\r\n at System.IO.StringWriter.ToString()\r\n at JSonExtensions.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)The same works fine with retry. Updated the configuration to support large objects, but still no luck . Any help is greatly appreciated.
Thanks.
Thursday, April 27, 2017 9:03 PM
All replies
-
User281315223 posted
What does your actual code look like?
If you are getting an out of memory exception, then you are likely in some type of infinite loop. Is there any chance that the object that you are attempting to serialize has some type of self-referential properties or is it some type of graph that might be cyclical?
If that's the case, be sure that you are serializing / deserializing with JSON.NET configured to handle these types of scenarios:
JsonSerializerSettings settings = new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects }; // Create your serializer and then do stuff var serializer = JsonSerializer.Create(settings);
It looks like you are trying to serialize a StringBuilder object, but without seeing your code, it's tough to tell. Consider posting your code and it'll be easier to diagnose your issue.
Friday, April 28, 2017 2:27 PM -
User1247222288 posted
Hi Rion,
Thanks for the reply.
This is mostly happening when cloning an object : JSon.Deserialize<object>(JSon.Serialize(this)).
The exception was thrown when serializing using Newtonsoft.Json library .
"Message": "Exception of type 'System.OutOfMemoryException' was thrown.",
"StackTrace": " at System.Text.StringBuilder.ToString()\r\n at System.IO.StringWriter.ToString()\r\n at JSonExtensions.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer) in ..Friday, April 28, 2017 7:55 PM -
User281315223 posted
This could be an issue of you actually trying to serialize the current object (this) and then deserialize it. Have you tried using something like MemberwiseClone() to make a copy or some other approach:
YourObjectType clone = (YourObjectType)this.MemberwiseClone();
Thursday, May 4, 2017 2:13 PM