Disable query plan execution of HQL in hibernate 5.2.11final at the time of application loading

Disable query plan execution of HQL in hibernate 5.2.11final at the time of application loading.

org.hibernate.loader.plan.exec.query.internal.SelectStatementBuilder.java
is responsible for increase in memory

You need to provide more context to your problem.

Is the memory issue due to the fact that you have many NamedQuery defined which Hibernate needs to parse and store so that you can alter use without reparsing them?

1 Like

Yes exactly this is what I want

yes I have many NamedQuery in my application and the memory consumption drastically increased after migrating to Hibernate 5.2.11FINAL from 4.1.9.Final

Upgrading to a newer version can solve many problems. Hibernate 4.1 is rather old, and many things have been improved since 4.x.

I have upgraded to 5.2.11Final and the memory consumption issue is occurring in 5.2.11

Try to profile it and see why it takes so much memory.

And when you say a lot of memory, how much is it taking? And how many named queries do you have?

hi vlad,
I did the profiling and observed that org.loader.plan.build.internal* package is consuming more memory and i compared it with older version (4.1.9) in which the above mention package is not present.

And number of named queries i have is 660 and number of entities are 450.

I am running batches using spring batch previously it required 512MB heap but now after migrating to hibernate 5.2.11 it requies 1024MB heap memory
please find the profiling screenshot for hibernate 5.2.11.Heap_New_Version

But in that diagram, the Query plan only takes 16 MB. Anyway, if it does not leak memory, just add sufficient RAM.

Nowadays, notebooks have 16 GB of RAM and servers way more than that. A 1 GB JVM is not out of the ordinary for an enterprise application.

The plan cache is just a cache. You can tune it to cache less and save memory, but it will cost you in terms of extra CUP processing.

Hi Vlad,

Thanks for your input and view will decrease the quert plan cache as you mentioned in the below thread
https://forum.hibernate.org/viewtopic.php?f=1&t=1044019

Sure thing. You can configure these two properties to adjust the query plan cache size.

  1. hibernate.query.plan_cache_max_size
  2. hibernate.query.plan_parameter_metadata_max_size
1 Like

Is there any way to disable this query plan cache?

If you set the hibernate.query.plan_cache_max_size to 0, it will disable the plan cache, but it’s not a good idea because the performance of JPQL and Criteria API will not be very good.

hibernate.batch_fetch_style =DYNAMIC fixed my issue

I also set plan_cache_max_size to a low number because we had many queries using batch updates like:

@Query("Update MyEntity m SET m.result = 1 WHERE m.id IN :ids")

and each one consumed few MB in cache.

Due to different lists of ids in their where clause, I suspect that caching query plan for batch updates does not bring any benefit because they won’t be re-used.

By the way @vlad , setting plan_cache_max_size to 0 is not possible because it fails while initializing a BoundedConcurrentHashMap with a concurrency level of 20, so the minimal value should be 40.

Caused by: java.lang.IllegalArgumentException: Maximum capacity has to be at least twice the concurrencyLevel
        at org.hibernate.internal.util.collections.BoundedConcurrentHashMap.<init>(BoundedConcurrentHashMap.java:1648)
        at org.hibernate.internal.util.collections.BoundedConcurrentHashMap.<init>(BoundedConcurrentHashMap.java:1722)
        at org.hibernate.engine.query.spi.QueryPlanCache.<init>(QueryPlanCache.java:119)
        at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:289)