Out of heap space during serialization

前端 未结 2 2013
暖寄归人
暖寄归人 2020-12-14 08:47

The following code is causing a OutOfMemmoryError: heap space for some 3 million rows.

Memory allocated to JVM is 4 GB, using 64 bit installation.

2条回答
  •  半阙折子戏
    2020-12-14 09:09

    Is objOS an ObjectOutputStream?

    If so, then that's your problem: An ObjectOutputStream keeps a strong reference to every object that was ever written to it in order to avoid writing the same object twice (it will simply write a reference saying "that object that I wrote before with id x").

    This means that you're effectively leaking all ArrayList istances.

    You can reset that "cache" by calling reset() on your ObjectOutputStream. Since you don't seem to be making use of that cache between writeObject calls anyway, you could call reset() directly after the writeObject() call.

提交回复
热议问题