Thursday, March 12, 2009

Why is OutOfMemoryError thrown after writing a large number of objects

Question :Why is OutOfMemoryError thrown after writing a large number of objects
into an ObjectOutputStream? (CoreJava)

Answer :The ObjectOutputStream maintains a table mapping objects written into
the stream to a handle. The first time an object is written to a stream, its
contents are written into the stream; subsequent writes of the object result
in a handle to the object being written into the stream. This table
maintains references to objects that might otherwise be unreachable by an
application, thus, resulting in an unexpected situation of running out of
memory. A call to the ObjectOutputStream.reset() method resets the
object/handle table to its initial state, allowing all previously written
objects to be elgible for garbage collection.

No comments: