Java 对象序列化性能提示 [英] Java Object Serialization Performance tips

查看:52
本文介绍了Java 对象序列化性能提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将一棵巨大的对象树 (7,000) 序列化到磁盘中.最初我们使用 Kodo 将这棵树保存在一个数据库中,但是将这棵树加载到内存中会产生成千上万的查询,并且会占用本地宇宙可用时间的很大一部分.

I must serialize a huge tree of objects (7,000) into disk. Originally we kept this tree in a database with Kodo, but it would make thousands upon thousands of Queries to load this tree into memory, and it would take a good part of the local universe available time.

我为此尝试了序列化,并且确实获得了性能改进.但是,我觉得我可以通过编写自己的自定义序列化代码来改进这一点.我需要尽快加载这个序列化对象.

I tried serialization for this and indeed I get a performance improvement. However, I get the feeling that I could improve this by writing my own, custom serialization code. I need to make loading this serialized object as fast as possible.

在我的机器上,序列化/反序列化这些对象大约需要 15 秒.从数据库加载它们时,大约需要 40 秒.

In my machine, serializing / deserializing these objects takes about 15 seconds. When loading them from the database, it takes around 40 seconds.

考虑到因为对象在树中,它们相互引用,所以我可以做些什么来提高这种性能,有什么提示吗?

Any tips on what could I do to improve this performance, taking into consideration that because objects are in a tree, they reference each other?

推荐答案

一种优化是自定义类描述符,以便您将类描述符存储在不同的数据库中,并且在对象流中您只能通过 ID 引用它们.这减少了序列化数据所需的空间.例如,请参阅一个项目中的类 SerialUtilClassesTable 这样做.

One optimization is customizing the class descriptors, so that you store the class descriptors in a different database and in the object stream you only refer to them by ID. This reduces the space needed by the serialized data. See for example how in one project the classes SerialUtil and ClassesTable do it.

使类可外部化而不是可序列化可以带来一些性能优势.缺点是需要大量的手动工作.

Making classes Externalizable instead of Serializable can give some performance benefits. The downside is that it requires lots of manual work.

然后还有其他序列化库,例如jserial,它可以提供比Java默认序列化更好的性能.此外,如果对象图不包含循环,那么它可以更快地序列化,因为序列化器不需要跟踪它所看到的对象(请参阅 jserial 的常见问题).

Then there are other serialization libraries, for example jserial, which can give better performance than Java's default serialization. Also, if the object graph does not include cycles, then it can be serialized a little bit faster, because the serializer does not need to keep track of objects it has seen (see "How does it work?" in jserial's FAQ).

这篇关于Java 对象序列化性能提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆