Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce Hibernate SessionFactory memory usage and load time

I have an application with about 3000 entity classes.

Due to the large number of classes the SessionFactory object consumes about 150 MB of memory and takes almost a minute to setup (process all classes, generate the proxies and building the meta model).
After profiling the process I've discovered that a minute of the time is spent in org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory while half is in org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(EntityMetamodel, PersistentClass).
I'm looking for ways to reduce the SessionFactory memory usage and load time.
One thought was to move the class enhancement to compile time, but I didn't find much information about it.
The number of entities is a fact that I can't change but if someone has better ideas on how to handle their database access (other than Hibernate) I'll be happy to hear about it.

like image 691
Avner Levy Avatar asked Dec 07 '25 01:12

Avner Levy


2 Answers

3000 classes just sound too crazy for me.
what do you mean by "class enhancement"?
I think you should carefully reconsider the design of your application.
Hibernate heavily uses Proxy objects (for example, when you run a setter, the Proxy "aggregates" this change, so Hibernate can generate an update SQL with only changed fields). Hibernate also uses class information in memory used by the proxies (as explained above) and this is why your memory grows like this

like image 83
Yair Zaslavsky Avatar answered Dec 08 '25 14:12

Yair Zaslavsky


Start by looking at what's happening inside the SessionFactory. Hibernate offers a JMX connector, see documentation here. Then you can start to look into your hotspots and how to refactor them. You really need to begin with gathering some more metrics. Right now it could be anything, including GC problems.

like image 45
Mirko Adari Avatar answered Dec 08 '25 14:12

Mirko Adari