Out of heap space during serialization

Nik picture Nik · Sep 21, 2011 · Viewed 9.2k times · Source

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.

while (rs.next())
{    
    ArrayList<String> arrayList = new ArrayList<String>();
    for (int i = 1; i <= columnCount; i++)
    {
        arrayList.add(rs.getString(i));
    }

    objOS.writeObject(arrayList);
}

The memory referenced by the ArrayList is eligible for garbage collection in each iteration of the while loop, and internally JVM calls garbage collection (System.gc()) before throwing an OutOfMemoryError because of heap space.

So why is the exception occurring?

Answer

Joachim Sauer picture Joachim Sauer · Sep 21, 2011

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.