Search code examples
javahibernatememory-leakshibernate-entitymanager

Memory leak in Hibernate session?


I am experiencing a memory leak in a Hibernate session.

A long running thread is continuously loading and updating data. While the thread regularily calls entityManager.clear() I observe both:

1.) growth in Session-size (linear growth since start of application)

2.) growth in execution-time (exponential growth since start of application)

The only thing, that seems to help is to close() the entitymanager and re-open it. I have seen this behaviour in several unconnected applications, some with spring, some without, some using the jpa-frontend, some using hibernate natively.

Weird enough: while I have never encountered a hibernate application without this problem, searching the internet comes up blank. What am I (and dozens of other developers) doing wrong?


Solution

  • There is a bug in hibernate that prevent clear for clearing session cleanly. See:

    It should be fixed in 4.3.

    My workaround is to use short-lived session.