[转帖]org.hibernate.engine.query.spi.QueryPlanCache引起内存泄漏问题排查

org,hibernate,engine,query,spi,queryplancache,引起,内存,泄漏,问题,排查 · 浏览次数 : 0

小编点评

**问题现象:** 一个后台服务在运行一段时间后,观察内存一直在增加,使用MAT工具,打开堆文件分析内存泄漏情况发现如下提示: ``` One instance of "org.hibernate.internal.SessionFactoryImpl" loaded by "org.springframework.boot.loader.LaunchedURLClassLoader @ 0x2c00204b8" occupies 8,137,929,888 (98.05%) bytes. The memory is accumulated in one instance of "org.hibernate.internal.util.collections.BoundedConcurrentHashMap$Segment[]" loaded by "org.springframework.boot.loader.LaunchedURLClassLoader @ 0x2c00204b8" Keywords org.hibernate.internal.util.collections.BoundedConcurrentHashMap$Segment[] org.springframework.boot.loader.LaunchedURLClassLoader @ 0x2c00204b8 org.hibernate.internal.SessionFactoryImpl Details » ``` **问题分析:** 问题原因是QueryPlanCache缓存中存在大量sql,由于在in子句后面跟不同数量或数值,会增加一条缓存。当缓存数量超过了5.1.1和 earlier版本的最大值时,会发生内存溢出。 **解决方案:** 1. 在application.properties文件中设置以下配置: ``` # Sets the maximum number of soft references held in the cache. spring.jpa.properties.hibernate.query.plan_cache_max_soft_references=1024 # Sets the maximum number of strong references held in the cache. spring.jpa.properties.hibernate.query.plan_cache_max_strong_references=64 ``` 2. 调整QueryPlanCache的缓存大小,使其不再过大。 **其他建议:** * 可以使用内存分析工具,例如GCROOT或JProfiler,进一步分析内存泄漏问题。 * 可以调整hibernate的配置参数,例如 `spring.jpa.properties.hibernate.query.plan_cache_max_size`。 * 可以使用缓存清理工具,例如 Spring Boot Actuator,定期清理缓存。

正文

问题现象: 

一个后台服务在运行一段时间后,观察内存一直在增加,使用MAT工具,打开堆文件分析内存泄漏情况:fileName_Leak_Suspects的index

 发现如下提示:

One instance of "org.hibernate.internal.SessionFactoryImpl" loaded by "org.springframework.boot.loader.LaunchedURLClassLoader @ 0x2c00204b8" occupies 8,137,929,888 (98.05%) bytes. The memory is accumulated in one instance of "org.hibernate.internal.util.collections.BoundedConcurrentHashMap$Segment[]" loaded by "org.springframework.boot.loader.LaunchedURLClassLoader @ 0x2c00204b8".

Keywords
org.hibernate.internal.util.collections.BoundedConcurrentHashMap$Segment[]
org.springframework.boot.loader.LaunchedURLClassLoader @ 0x2c00204b8
org.hibernate.internal.SessionFactoryImpl

Details »

 打开details,可以看到 org.hibernate.engine.query.spi.QueryPlanCache 占用了大量空间。问题就出在这里。

        hibernate中的QueryPlanCache会缓存sql,QueryPlanCache占用多大,基本上归结为IN子句中具有可变数量的值,而Hibernate试图缓存这些查询计划。如果in子句后面跟不同数量或数值,都会增加一条缓存(业务中确实有这么一条根据id进行记录更新操作)。从而缓存大量的sql导致heap内存溢出

解决方案:

 在application.properties文件增加以下配置:

  1. #manages the number of ParameterMetadata instances in the cache (defaults to 128)
  2. spring.jpa.properties.hibernate.query.plan_parameter_metadata_max_size=32
  3. #controls the maximum number of entries in the plan cache (defaults to 2048)
  4. spring.jpa.properties.hibernate.query.plan_cache_max_size=64
  5. #Sets the maximum number of soft references held in the cache. Set this value to Integer.MAX_VALUE to replicate the behavior of 5.1.1 and earlier. e.g. 2048 (default)
  6. spring.jpa.properties.hibernate.query.plan_cache_max_soft_references=1024
  7. #Sets the maximum number of strong references held in the cache. e.g. 128 (default)
  8. spring.jpa.properties.hibernate.query.plan_cache_max_strong_references=64
  9. spring.jpa.properties.hibernate.query.in_clause_parameter_padding=true

文章知识点与官方知识档案匹配,可进一步学习相关知识
Java技能树首页概览97715 人正在系统学习中

与[转帖]org.hibernate.engine.query.spi.QueryPlanCache引起内存泄漏问题排查相似的内容:

[转帖]org.hibernate.engine.query.spi.QueryPlanCache引起内存泄漏问题排查

事情缘由 协助公司中药网排查内存溢出的问题。这个系统是基于Hibernate3开发的一个药品信息、价格变动、药品咨询新闻相关的网站,90%的操作都是查询。 问题日志 工具排查 Dump堆内存文件,使用MAT进行分析 1、jsp 命令获取应用的pid 2、jmap -dump:format=b,fil

[转帖]org.hibernate.engine.query.spi.QueryPlanCache引起内存泄漏问题排查

问题现象: 一个后台服务在运行一段时间后,观察内存一直在增加,使用MAT工具,打开堆文件分析内存泄漏情况:fileName_Leak_Suspects的index 发现如下提示: One instance of "org.hibernate.internal.SessionFactoryImpl" 

[转帖]关于https://goproxy.cn,direct与https://proxy.golang.org的问题,国内无法访问https://proxy.golang.org设置了GOPROXY仍不可行

关于https://goproxy.cn,direct与https://proxy.golang.org的问题,国内无法访问https://proxy.golang.org设置了GOPROXY仍不可行 一步一步说: 首先,遇到报错信息 go: github.com/StackExchange/wmi

[转帖]Windows Server 2022 简体中文版、英文版下载 (updated Oct 2022)

https://sysin.org/blog/windows-server-2022/ Windows Server 2022 正式版,2022 年 10 月更新,VLSC Posted by sysin on 2022-10-27 Estimated Reading Time 8 Minutes

[转帖]Office LTSC 2021 发布

https://sysin.org/blog/office-2021-iso/ 2021 年 9 月 16 日,微软正式发布了支持 Office 2021 的部署工具(Office Deployment Tool),这意味着 Office 2021 已经正式发布,当然系统要求仅支持 Windows

[转帖]QUIC & HTTP/3 Support:主流浏览器和服务端对 HTTP/3 的支持情况(2021年12月更新)

https://sysin.org/blog/quic-http3-support/ 1. 相关概念 1.1 TLSv1.3 TLS 1.3 由 IETF 于 2018 年 8 月正式发布。 SSL 即 Secure Sockets Layer 安全套接字层。TLS 即 Transport Laye

[转帖]Microsoft SQL Server 下载汇总

https://sysin.org/blog/sql-server/ 下载链接 SQL Server Management Studio (SSMS) 18 SQL Server 2022 预览版发布:基于 Azure 的持续性能和安全创新 Microsoft SQL Server 2019 Mic

[转帖]Firefox 105,Chrome 105,Edge 105 官网离线下载 (macOS, Linux, Windows)

https://sysin.org/blog/chrome-edge-firefox-download/ 备忘一下, 天下只剩三种(主流)浏览器: Apple Safari Mozilla Firefox Google Chrome(Chromium)【国外各种(Microsoft Edge、Ope

[转帖]HTTP 安全响应头(Security Response header)配置手册

https://sysin.org/blog/security-headers/ 一、常用安全 Header 释义 1. Strict-Transport-Security (HSTS) HTTP Strict Transport Security(通常简称为 HSTS)是一个安全功能,它告诉浏览器

[转帖]Windows Server 2025 简体中文版下载(Inside Preview),下一代 Windows 11 Server

https://sysin.org/blog/windows-server-2025/ 我们知道 Windows Server 2022 仍然属于 Windows 10 Server 的范畴。现在,下一代基于 Windows 11 的 Windows Server 已经可以公开下载,根据 A3 的产