Hibernate statistics grows QueryStatistics ConcurrentHashMap indefinitely


We’ve encountered a problem with the generate_statistics configuration in production.

The ConcurrentStatisticsImpl uses a ConcurrentHashMap that saves the statistics for all the HQL queries that are executed. However from our application we generate a lot of unique query HQL queries because we use the JPA query builder, and something like this:

	public void query(int id, int userId) {
		// ...
		CriteriaBuilder builder = session.getCriteriaBuilder();
		CriteriaQuery<InboxMessage> query = builder.createQuery(InboxMessage.class);
		Root<InboxMessage> root = query.from(InboxMessage.class);
			builder.equal(root.get(InboxMessage_.id), id)),
			builder.equal(root.get(InboxMessage_.inboxUser).get("id"), userId));
		// ...

creates a new query string for each new input.

This is problematic because the map only grows, and after a couple of days our application consumes too much memory.

So what I’m wondering is:

  • Should we rewrite all our queries to HQL strings with parameters such as :id, :userId?
  • Or can we map similar queries to the same string?
  • Or is there a way to clear the statistics regularly, or make them bounded
  • Or should we just disable statistics?
  • Something else?

You can call clear periodically on the Statistics object reference.

You could also add a Jira issue so we can enhance the Statistics to limit the size.

Thanks for the reply. We’ll just disable the statistics for now in production as we’ve not used them a lot anyway :sweat_smile:

Added a Jira issue: https://hibernate.atlassian.net/browse/HHH-12933