Saturday, May 7, 2011

Grails memory leak on batch inserts and a possible OOM

Recently, I worked on downloading a subset of our huge database into a grails' HSQL. Initially, I thought this was so easy that it almost qualifies as hello world2.

But I hit a roadblock. The job that downloads rows and saves to the HSQL would go out of memory after a 100K rows or so. Specifially, it would throw "java.lang.OutOfMemoryError: Java heap space". I was certain that this was memory failing to get recollect. But without going through the memory analysis, I went after obvious suspects: hibernate session and HSQL transactional memory. I took clues from here and here. But no avail. Finally, this post from Burt Beckwith explained it all.

All I needed to do was set the 'errors' to null after saving domain objects.

To summarize the leak: Grails keeps references to the errors objects that are on the domain objects in the httprequest. So, as long as a request is around, the domain objects will not be garbage collected. Makes sense? :)

No comments:

Post a Comment