What you're seeing there, it's not the
second-level Query cache, but the HQL Query Plan Cache. The HQL Query Plan Cache is used to reuse the parsing phase of the entity query AST.
If the Query Plan Cache consumes a lot of memory, it could be because you are generating lots of HQL queries. Are you using bind parameters or every entity query contains the parameter values inlined? If you don't use bind parameters, you are
risking SQL Injection attacks.
You can control the Query Plan Cache using the following
two configuration properties:
1. hibernate.query.plan_cache_max_size
2. hibernate.query.plan_parameter_metadata_max_size
However, there's a trade-off between memory consumption and CPU, so make sure that you don't set those too low as you might turn your memory issue into a CPU bottleneck.