Hibernate memory management

If you get memory issues and the SessionFactory seems to be the culprit, then it can be due to the following reasons:

  1. Either you have enabled the 2nd-level cache and cached all entities and configured Ehcache to store all entities in-memory. Try to limit the memory size occupied by your cached entries to something more reasonable.
  2. The underlying Session objects are too large because you are fetching millions or records with your queries. Make sure you use pagination in your queries.
  3. You have configured the JVM to use a tiny amount of maximum allocated memory and you should raise the Xmx JVM flag.