Serialize cyclic object tree – StackOverflowError – custom serialization code needed

偶尔善良 提交于 2019-12-06 14:14:31

Java serialization handles cyclic graphs correctly, but long lists are a problem.

If I understand correctly, your problem is a bit different than the one in the article you linked, cause that article talks about linked lists without a proper writeObject .. while currently you are using ArrayLists, which already stores objects as a flat array, and also have a proper writeObject.

However, if I got it right, what happens is more or less this :

You start serializing object A, during that serialization it encounters a long list, takes the first element and serializes object B, object B also has a long list, it takes the first element, say it's A again, since it is already being serialized it skips A, so it does not end up in an endless loop, however then goes to the second object in the list of B, which is C, also C has a long list, the first two elements are A and B again, so they are skipped, the third is D, which also has a list .... and soooo on.

Since each one of these steps are a few lines in the stack stace, it fills up, even if they are all ArrayLists with a proper writeObject method.

This can be the case if those lists scale into relatively "big" numbers, compared to the runtime you have. That's why I asked in the comment. Maybe a solution can be found avoiding to serialize some lists (making them transient), and rebuilding them after the "partial" deserialization.

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