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.
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.
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 ..
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:
Member
334 Points
135 Posts
Out of memory exception c#
Apr 27, 2017 09:03 PM|vijay1243|LINK
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.
Regards,
VijayReddy
All-Star
114593 Points
18503 Posts
MVP
Re: Out of memory exception c#
Apr 28, 2017 02:27 PM|Rion Williams|LINK
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:
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.
Member
334 Points
135 Posts
Re: Out of memory exception c#
Apr 28, 2017 07:55 PM|vijay1243|LINK
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 ..
Regards,
VijayReddy
All-Star
114593 Points
18503 Posts
MVP
Re: Out of memory exception c#
May 04, 2017 02:13 PM|Rion Williams|LINK
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: