How to serialize/deserialize complex java object quickly

白昼怎懂夜的黑 提交于 2019-12-22 17:48:19

问题


I'm debugging and fixing a complex app, that works with a huge Java object (~250M).

I've created this object with another program. Currently I use XStream to load and save this object from the hard drive, but it takes more than a minute to parse it. It slows down the development process.

Is JAXB faster? Are there any other ways to load and save this huge thing?


回答1:


In that case I would Serialize the data which will make it smaller and faster. You can Externalise key classes to improve the speed further. Here are some test I did more recently Protobuf vs Thrift vs Java Serialization Its the same benchmark as kovica suggest but run more recently on newer hardware/software.

If you need to go faster you can use memory mapped data. This is much harder to work with but you can't beat the performance. You can load 250 MB is tens of milli-seconds if the data is already in disk cache. If not you will be limited by the speed of your drive e.g. a slow 40 MB/s drive will take about 8 seconds.

A library I wrote uses memory mapped files is Java Chronicle




回答2:


I've never used XStream, but according to their documentation it produces XML, right? Do you need to store those objects in XML? If you just need to store object to disk then there are couple of different approaches you can use:

  • java serialization/externalization
  • kryo
  • google protobut
  • ...

Look also here: High performance serialization: Java vs Google Protocol Buffers vs ...?




回答3:


I would recommend Java serialization to others. It can take the advantage of reusing the objects in heap (eg: String's intern() method) instead of creating a new objects (like other serializers generally do). This reduces the total amount of payload.



来源:https://stackoverflow.com/questions/12435491/how-to-serialize-deserialize-complex-java-object-quickly

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