We have a dynamic web project 3.0 using java server face 2.2 and JPA 2.1,Hibernate 4.2.3 where multiple session factory connected to multiple identical database is created using NON-JTA data source. We use multi tenancy architecture. After analyzing the heap dump of the said application, it had been seen that, each session factory is holding all entity classes of our web application which is consuming lots of memory. Lets consider the following actual running scenario for better understanding,
Our Application generate 200 session factory for 200 database. Our Application contain total 420 entity class. After analyzing heap dump it had been seen that, total 200 sessionfactory were generated which were holding instances of all 420 entity classes individually so, each session factory taking around 80 MB memory. So the total memory consumption becomes 16 GB. Which is exceptionally high consumption.
In another scenario, when we reduced no of entity from 420 to 1, then all session factory for 200 database taking memory which is minimal and acceptable for our case.
So,we think session factory consuming too much memory when we add more entity classes, our query is there any possible ways to share those entity classes between all sessionfactory or any other way is possible to reduce memory consumption ?
For your reference we are attached a heap dump screen shot for three sessionfactory.
Looking for any positive replies. Any quick replies will be greatly helpful.