Hibernate memory management

Hi all, I am getting memory issues by using hibernate in my web application. After a lot of searching found a tool ‘Ecplise Memory Analyzer’ and found 3 suspects regarding the use of SessionFactory. Please suggest me how to manage it and some coding standards for hibernate dao class. I am pasting some screen shots of them:

Problem Suspect 1
One instance of “org.hibernate.internal.SessionFactoryImpl” loaded by “org.apache.catalina.loader.ParallelWebappClassLoader @ 0x3406b1328” occupies 13,949,880 (31.10%) bytes. The memory is accumulated in one instance of “org.hibernate.internal.SessionFactoryImpl” loaded by “org.apache.catalina.loader.ParallelWebappClassLoader @ 0x3406b1328”.

Keywords
org.apache.catalina.loader.ParallelWebappClassLoader @ 0x3406b1328
org.hibernate.internal.SessionFactoryImpl

Problem Suspect 2
The classloader/component “org.apache.catalina.loader.ParallelWebappClassLoader @ 0x3406b1328” occupies 4,831,120 (10.77%) bytes. The memory is accumulated in classloader/component “org.apache.catalina.loader.ParallelWebappClassLoader @ 0x3406b1328”.

Keywords
org.apache.catalina.loader.ParallelWebappClassLoader @ 0x3406b1328

Problem Suspect 3
5,985 instances of “java.lang.Class”, loaded by “” occupy 7,111,368 (15.85%) bytes.

Biggest instances:
•class java.lang.ref.Finalizer @ 0x340000668 - 1,771,352 (3.95%) bytes.

Keywords
java.lang.Class

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.