Deserialize from MemoryStream throws OutOfMemory exception in C#

心已入冬 提交于 2019-12-12 19:58:02

问题


This is a C# 3.0 Winform problem.

I want to clone a big(not very big actually), complicated object. To do it lazily, I seralize it to a MemoryStream and use BinaryFormatter.UnsafeDeserialize(MemoryStream, null) method to get the cloned object. Inside the UnsafeDeserialize() method, there is always an OutOfMemory exception thrown. The memory should be sufficient.

I read a bit about this as a .net bug but not sure whether it is the case here.

Anyone has any idea why this is happening? Any workround? Thanks.


回答1:


Perhaps a silly question but are you remembering to rewind the MemoryStream to the start of the stream before deserializing it?

It might also help to share some of your code.




回答2:


Why are you using UnsafeDeserialize instead of Deserialize?




回答3:


What is the object, and how big is it?

I've seen other people with a similar problems, and in some cases switching to a different serializer fixed it. I can suggest several that may be a good fit, depending on the scenario; in particular - is the data a tree or a graph? (i.e. do child objects ever refer to siblings or their parents? Or just their own children).




回答4:


I suggest you try a FileStream instead, and see how big the file is.



来源:https://stackoverflow.com/questions/1136061/deserialize-from-memorystream-throws-outofmemory-exception-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!