I am coding a game using LibGdx. I created a level editor where you can place objects for the level. All the objects are put into a list and then the whole list is serialized by writing out the list to an object output stream. I then read in the list of objects in my game and copy the list over to my list of objects in the current level being played. On the actual user's device who is playing the game, there will be 20+ serialized level files. They will only be deserialized at this point. Is this an efficient way to do this in regards to memory and performance? Could those files take up a good chunk of memory? I noticed people use xml or Json for what I am doing. Should I be worried about having any issues the way I have done my level loading? Thanks. Let me know if my question isn't clear.
-
Why would files take up any memory at all? They're files.user207421– user2074212014-12-23 01:31:21 +00:00Commented Dec 23, 2014 at 1:31
-
Don't worry about performance issues until you actually find that a particular thing is a problem. Using a textual format makes debugging both ends of the software dramatically easier, and reading or writing JSON and XML usually isn't a major source of overhead.chrylis -cautiouslyoptimistic-– chrylis -cautiouslyoptimistic-2014-12-23 08:03:54 +00:00Commented Dec 23, 2014 at 8:03
2 Answers
When we were looking for slowdowns in our code we profiled it running and found a huge amount of time being spent on one thing. When we looked it turned out to be a section where an object was being duplicated by serializing it to a string and back. For some reason, the default java implementation of serialization is SLOW.
The other problem with serialization is that it's black box--if your file gets corrupted or you mess up your object enough during an upgrade you can lose everything.
Did you consider ORM and some kind of easy database? There are databases that are compiled into your code (invisible to your user) and it's pretty much just db.save(anyObject)... very easy to use.
Comments
for a similiar question I did a short research (because I couldn't believe object serialization is slow) and i would recommend you to use JSON, as it's faster. Memory usage in terms of RAM will be the same (as soon as your object is deserialized). On disk you might want to zip it.
according to those benchmarks, jackson is faster than java serialization: benchmark deeplink
another advantage is the human readability of your json files.