Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MemoryLeak: Hibernate Sessions #2870

Closed
fviale opened this issue May 23, 2017 · 2 comments
Closed

MemoryLeak: Hibernate Sessions #2870

fviale opened this issue May 23, 2017 · 2 comments

Comments

@fviale
Copy link
Member

fviale commented May 23, 2017

Analyse of a heap dump shows a large heap memory usage due to hibernate session factory and QueryPlanCache from SchedulerDBManager

Class Name                                                                                       | Shallow Heap | Retained Heap
--------------------------------------------------------------------------------------------------------------------------------
org.hibernate.internal.SessionFactoryImpl @ 0x6db944410                                          |          136 |   445 322 392
|- <class> class org.hibernate.internal.SessionFactoryImpl @ 0x6db955028                         |           16 |            96
|- dialect org.hibernate.dialect.HSQLDialect @ 0x6db944358                                       |           72 |         6 040
|- serviceRegistry org.hibernate.service.internal.SessionFactoryServiceRegistryImpl @ 0x6db9443b0|           48 |        11 032
|- properties java.util.Properties @ 0x6db9443e0                                                 |           48 |         3 616
|- sessionFactoryOptions org.hibernate.boot.internal.SessionFactoryOptionsImpl @ 0x6db944498     |          160 |           536
|- jdbcServices org.hibernate.engine.jdbc.internal.JdbcServicesImpl @ 0x6db9a90b0                |           32 |            64
|- uuid java.lang.String @ 0x6db9a9cd8  e278da6b-637c-4fbe-b870-6b1868b68398                     |           24 |           112
|- cacheAccessStrategiesMap java.util.HashMap @ 0x6db9d9fb8                                      |           48 |            48
|- typeHelper org.hibernate.internal.TypeLocatorImpl @ 0x6db9d9fe8                               |           16 |            16
|- typeResolver org.hibernate.type.TypeResolver @ 0x6db9d9ff8                                    |           24 |         6 280
|- queryPlanCache org.hibernate.engine.query.spi.QueryPlanCache @ 0x6db9daaa0                    |           32 |   443 339 416
|- entityNameResolvers java.util.concurrent.ConcurrentHashMap @ 0x6db9dde50                      |           64 |            88
|- observer org.hibernate.internal.SessionFactoryObserverChain @ 0x6db9dde90                     |           16 |           200
|- namedQueryRepository org.hibernate.internal.NamedQueryRepository @ 0x6db9dfe60                |           32 |        16 264
|- collectionRolesByEntityParticipant java.util.Collections$UnmodifiableMap @ 0x6db9dff50        |           32 |         1 616
|- collectionMetadata java.util.Collections$UnmodifiableMap @ 0x6db9dff70                        |           32 |           416
|- collectionPersisters java.util.HashMap @ 0x6db9dffb0                                          |           48 |           400
|- cacheAccess org.hibernate.internal.CacheImpl @ 0x6db9e26e0                                    |           40 |           104
|- sqlFunctionRegistry org.hibernate.dialect.function.SQLFunctionRegistry @ 0x6db9e4df8          |           16 |         3 104
|- filters java.util.HashMap @ 0x6db9e5928                                                       |           48 |            48
|- identifierGenerators java.util.HashMap @ 0x6db9e5998                                          |           48 |           448
|- classMetadata java.util.Collections$UnmodifiableMap @ 0x6db9e5b58                             |           32 |           512
|- entityPersisters java.util.HashMap @ 0x6db9e5b78                                              |           48 |           464
|- settings org.hibernate.cfg.Settings @ 0x6db9ea160                                             |           24 |            24
|- imports java.util.HashMap @ 0x6db9ea2e8                                                       |           48 |         1 536
'- Total: 25 of 27 entries; 2 more                                                               |              |              
--------------------------------------------------------------------------------------------------------------------------------

The session factory maintains a cache of compiled queries. If this cache is not cleaned up, it keeps growing over time

A discussion maybe/maybenot linked:
https://stackoverflow.com/questions/17823599/possible-memory-leak-due-to-org-hibernate-internal-sessionfactoryimpl

A discussion most likely linked:
https://stackoverflow.com/questions/31557076/spring-hibernate-query-plan-cache-memory-usage

@yinan-liu
Copy link
Contributor

yinan-liu commented May 23, 2017

if we instantiate a session factory once and only once (e.g. SchedulerDBManager is a singleton), it is ok, we use factory to create the session and close the session when it is finish. If we keep creating factory or never close the session, than we will have problem

@fviale
Copy link
Member Author

fviale commented May 23, 2017

Indeed the problem does not seem to come from Session, but from QueryPlanCache.

This suggestion from the attached thread seems to be pertinent:

Try adding the new properties hibernate.query.plan_cache_max_size and hibernate.query.plan_parameter_metadata_max_size

It seems to work for now, if I set those parameters very low (16 and 128)

@fviale fviale closed this as completed Jun 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants