H2 Memory Leak

I became a witness to really weird behaviour of the H2 database recently: application heap would grow almost without limit. Why I say almost? Well, because now and then Garbage collection will reclaim a huge portion of memory and the application will run fine for a while.

Since there was a lot of stuff component in the application, getting down to the bottom of it was really hard. I will save you trouble and and just share this link. This is commit where H2 introduced an expiry timeout to object which H2 uses internally to represent BLOBs and CLOBs (Character Large OBjects if you wonder). This feature means that H2 will hold on to the retrieved CLOB/BLOBs for 5 minutes before the object will be available to be reclaimed by GC. More objects you generate, more objects you will leave in memory. This also explaied why periodically there was GC reclaiming a chunk of memory, it was after 5 minutes of initial object creation.

Good news that you can control this value in JDBC url in this way:


The value is in milliseconds. Enjoy!

I will get in touch with H2 team and see if they know about this feature and if should be documented better.