October 22, 2003

Hibernate OutOfMemoryError

This afternoon Alan and I worked out what was causing some OutOfMemoryErrors that have been persisting (excuse the pun) for some time.

It would seem that one has to be careful when mapping an association as a list in Hibernate.

As we found out, the combination of an inappropriate List mapping and a very large value (in our case over 10 million) of an index was leading to the OutOfMemoryErrors. Changing the mapping to a bag did the trick in our case.

I notice on the Hibernate Forums that several other people have experienced OutOfMemoryErrors with Hibernate. Perhaps the lesson is to check the mappings you are using for collections carefully to make sure they are appropriate.

Posted to Software Development by Keith Pitty
Comments

I'd say you should first check why you load 10 mio objects into memory? Of course you get OutOfMemory if you are not careful. We are a bit tired of "Bug in Hibernate! I'm loading my complete harddisk and get OutOfMemory!" postings actually. Just because it works like magic doesn't mean it actually is. ;)

Posted by: Christian Bauer at October 22, 2003 10:56 PM

Well I got some OOME from Hibernate too, which stopped my application (Linux did that). I did not use 10 Mio objects though ;-)

Posted by: Stephan Schmidt at October 22, 2003 11:29 PM

Christian: The error was not related to loading too many objects from the database. Only three or four objects were actually being put in the association.

The error in this case was that the "index" of the list association had been incorrectly mapped to the primary key of the table. For various reasons, the value for the primary keys of this table started at around 10,000,000. As such, Hibernate was telling the JVM to allocate an almost completely null-populated ArrayList... with 10,000,000 slots.

This is a very subtle mapping misuse, because 99% of the time the program will work perfectly (albeit slower than it should). So long as your primary key values are low, and/or you have a large heap. If your heap gets tight or your primary keys grow too high, though... boom.

Posted by: Charles Miller at October 23, 2003 12:01 AM

That sounds like a misuse of the primary key as list index. You're attaching some sort of business meaning (ordering) to the primary key, which is almost always a bad idea.

Posted by: Jesse Blomberg at October 23, 2003 2:26 AM

Charles: Thanks for your clarifying comment.

Christian: Perhaps this is a topic that should be included in one of the FAQs.

Posted by: Keith Pitty at October 23, 2003 12:24 PM