4

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?

Jonathan
  • 2,698
  • 24
  • 37
  • hibernate sessions should not run a *long* time, it should be closed in the shot-circuit. – Roman C Aug 15 '13 at 13:29
  • the hibernate session is bound to the entitymanager and I can't find a possibility to put a different one into the em. At the same time, the entitymanager is bound to the running thread (at least: Spring says so). So: Does the use of hibernate forbid long-running threads? – Jonathan Aug 15 '13 at 13:34
  • How it's bound? Do you obtain it from Spring? – Roman C Aug 15 '13 at 13:38
  • Yes, using the SpringEntityManagerFactoryUtils (at least in one of the applications) – Jonathan Aug 15 '13 at 13:48

1 Answers1

3

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.

ThomasEdwin
  • 2,035
  • 1
  • 24
  • 36