Skip to main content
gamedev.net
🔒 Locked

[.net] c# binary serialization memory usage

Started by bioxx Nov 28, 2011 at 5:41 PM 4 replies 1.9k views
Original Post
bioxx
bioxx
So I have a project that I'm toying with really and I've hit a major problem. The project is a hacking sim game using winforms. I have my program generate a massive amount of data about virtual people and computers and then I save the data to a file using binary serialization so that the player can play again later. If I set the program to create 50k people, it uses about 250MB of ram. When i go to load the 77MB save file, the amount of memory required to load the data explodes to 1.38GB. Also the program doesn't dispose of any memory after the loading is done. What could be causing the ballooning of memory usage? I've got rudimentary logging in place and to my knowledge there is no data duplication or anything like that occurrin. I'm really stumped.

Edit: I suppose I should note that I am a bit of a newb when it comes to serialization. Also of note is that I have a single game class that I serialize to a file. This Class contains several lists that cross reference each other. I am under the impression that the .net binary formatter handles this appropriately, am I wrong?
CadetUmfer
CadetUmfer
What method are you using to serialize/deserialize? By default .NET does not support circular references. Try using DataContractSerializer while setting preserveObjectReferences to true.
Anthony Umfer
TheTroll
TheTroll

So I have a project that I'm toying with really and I've hit a major problem. The project is a hacking sim game using winforms. I have my program generate a massive amount of data about virtual people and computers and then I save the data to a file using binary serialization so that the player can play again later. If I set the program to create 50k people, it uses about 250MB of ram. When i go to load the 77MB save file, the amount of memory required to load the data explodes to 1.38GB. Also the program doesn't dispose of any memory after the loading is done. What could be causing the ballooning of memory usage? I've got rudimentary logging in place and to my knowledge there is no data duplication or anything like that occurrin. I'm really stumped.

Edit: I suppose I should note that I am a bit of a newb when it comes to serialization. Also of note is that I have a single game class that I serialize to a file. This Class contains several lists that cross reference each other. I am under the impression that the .net binary formatter handles this appropriately, am I wrong?


Never assume anything when comes to serialization. The reality is that the seralizer has no idea what you really want, so it tires to do the best it can. With the situation you have, I would create my over seralizer.
bioxx
bioxx

What method are you using to serialize/deserialize? By default .NET does not support circular references. Try using DataContractSerializer while setting preserveObjectReferences to true.

I'm using Binaryformatter.serialize and Binaryformatter.deserialize while writing to a filestream. So with the DataContractSerializer all I need to do is assign [DataContract] to each class and [DataMember] to each field correct?



Never assume anything when comes to serialization. The reality is that the seralizer has no idea what you really want, so it tires to do the best it can. With the situation you have, I would create my over seralizer.

Well i only assumed after looking it up and finding vague answers that I thought were affirmation of my assumption :/
bioxx
bioxx
I'm not using anything precise, purely just task manager, but the amount of memory usage is so staggering that it seems like something must be wrong.

Edit: Upon further investigation I've found that the garbage collector doesn't immediately run after deserialize has been called. That would explain why the memory usage doesn't want to go back down. My next question would be, is there a more memory efficient way to save and load data with a complex object grid that using a binary formatter? I'd like to be able to continue to use the very large amount of data that I create, but needing 1.3GB of ram just to load the save file is excessive, clearly. Any pointers would be much appreciated, thanks.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.