Today I had an interesting debugging exercise, and I felt like I learned a new lesson that’s worth sharing with the rest of the world.
I had the following code, which takes a small-ish byte array and deserializes it into an object (let’s say someNotTooBigData is something like new byte[]{1,5,4, … some data… }.) Seems innocent enough, no?
<strike>void</strike>Object foo() {
byte[] buf = someNotTooBigData();
return new ObjectInputStream(new GZIPInputStream(
new ByteArrayInputStream(buf))).readObject();
}
But when this is executed frequently enough, like while(true) { foo(); }, it creates OutOfMemoryError. Can you tell why? I’ll post the answer tomorrow.