【内存泄漏】数据库连接connectionPhantomRefs内存过大

1. 问题背景

线上出现内存报警,内存增长曲线如下
在这里插入图片描述
dump内存文件,临时重新发布服务。后经排查发现是数据库连接池设置不合理以及mysql-connector-java 5.1.49有内存泄漏bug。以下为对此问题的分析及问题总结。

1.1 应用背景

数据库连接池: HikariCP
mysql-connector-java : 5.1.49版本
HikariCP配置:
在这里插入图片描述

2.问题分析

2.1 dump分析

通过 MAT 工具分析发现,com.mysql.jdbc.NonRegisteringDriver$ConnectionPhantomReference 实例较多且无法回收 如下图:
在这里插入图片描述
然后看大对象列表,NonRegisteringDriver 对象确实占内存比较多,其中成员变量connectionPhantomRefs占内存最多,里面存的是数据库连接的虚引用,其类型是 ConcurrentHashMap<ConnectionPhantomReference, ConnectionPhantomReference>,占比最多

2.2 源码分析

mysql创建连接源码:
com.mysql.jdbc.NonRegisteringDriver#connect

 public Connection connect(String url, Properties info) throws SQLException {if (url != null) {if (StringUtils.startsWithIgnoreCase(url, "jdbc:mysql:loadbalance://")) {return this.connectLoadBalanced(url, info);}if (StringUtils.startsWithIgnoreCase(url, "jdbc:mysql:replication://")) {return this.connectReplicationConnection(url, info);}}Properties props = null;if ((props = this.parseURL(url, info)) == null) {return null;} else if (!"1".equals(props.getProperty("NUM_HOSTS"))) {return this.connectFailover(url, info);} else {try {// 这里创建连接com.mysql.jdbc.Connection newConn = ConnectionImpl.getInstance(this.host(props), this.port(props), props, this.database(props), url);return newConn;} catch (SQLException var6) {throw var6;} catch (Exception var7) {SQLException sqlEx = SQLError.createSQLException(Messages.getString("NonRegisteringDriver.17") + var7.toString() + Messages.getString("NonRegisteringDriver.18"), "08001", (ExceptionInterceptor)null);sqlEx.initCause(var7);throw sqlEx;}}}

而 ConnectionImpl类在初始化构造的时候会调用NonRegisteringDriver.trackConnection(this);方法,而这个方法我们看命名就知道是追踪数据库连接的,我们接着往下看

public class NonRegisteringDriver implements Driver {protected static final ConcurrentHashMap<ConnectionPhantomReference, ConnectionPhantomReference> connectionPhantomRefs = new ConcurrentHashMap();
protected static final ReferenceQueue<ConnectionImpl> refQueue = new ReferenceQueue();protected static void trackConnection(com.mysql.jdbc.Connection newConn) {// 就是这里声明虚引用ConnectionPhantomReference,放入ReferenceQueueConnectionPhantomReference phantomRef = new ConnectionPhantomReference((ConnectionImpl)newConn, refQueue);connectionPhantomRefs.put(phantomRef, phantomRef);}}

第一行代码声明了一个名为connectionPhantomRefs的ConcurrentHashMap容器,该容器用于存储ConnectionPhantomReference实例。

第二个方法trackConnection的作用是将新连接添加到connectionPhantomRefs映射中。它接受一个com.mysql.jdbc.Connection对象作为参数,创建一个新的ConnectionPhantomReference实例,并使用它和引用队列(refQueue)将其添加到connectionPhantomRefs映射中。

总的来说,这两个代码片段旨在通过使用虚引用来实现跟踪连接到MySQL数据库的机制。虚引用用于跟踪已被JVM垃圾回收的对象,允许程序在对象从内存中删除后执行特定任务。

 static class ConnectionPhantomReference extends PhantomReference<ConnectionImpl> {private NetworkResources io;ConnectionPhantomReference(ConnectionImpl connectionImpl, ReferenceQueue<ConnectionImpl> q) {super(connectionImpl, q);try {this.io = connectionImpl.getIO().getNetworkResources();} catch (SQLException var4) {}}void cleanup() {if (this.io != null) {try {this.io.forceClose();} finally {this.io = null;}}}}

ConnectionPhantomReference后置处理如上,leanup() 方法用于在连接对象被垃圾回收后清理网络资源。它检查 io 属性是否为空,如果不为空,则调用 forceClose() 方法来强制关闭底层网络资源,最终将 io 属性设置为 null。整个过程确保连接对象被垃圾回收时,底层网络资源也被正确地释放。

MySQL为什么要使用虚引用来解决IO资源回收问题?

MySQL 使用虚引用来解决 IO 资源回收问题,主要是因为 JDBC 连接对象在关闭连接时无法保证其底层网络资源会被立即释放。这可能会导致底层网络资源长时间占用,最终导致应用程序出现性能下降或者资源耗尽的情况。

使用虚引用的好处在于,它允许程序在对象从内存中删除后执行特定任务。 MySQL 驱动程序利用 Java 提供的引用队列机制,将 JDBC 连接对象的虚引用加入到队列中。一旦连接对象被垃圾回收,JVM 会将它加入到引用队列中等待进一步处理。此时,MySQL 驱动程序通过监视引用队列并清理底层网络资源,确保这些资源在连接对象被垃圾回收时被正确地释放,从而避免了底层网络资源长时间占用的问题。

以下是虚引用的主要特点:

1.不影响对象的生命周期: 虚引用的存在并不会延长对象的生命周期。即使对象被虚引用引用着,只要没有其他强引用、软引用或者弱引用指向该对象,它也会被垃圾回收器回收。
2.用于跟踪对象的回收状态: 虚引用主要用于跟踪对象被回收的状态。当对象被垃圾回收器回收时,虚引用会被添加到与之关联的引用队列中,可以通过检查引用队列来得知对象已经被回收。
3.无法通过虚引用获取对象: 虚引用不可以直接通过 get() 方法获取到对象的引用,它的 get() 方法始终返回 null。因此,虚引用主要用于进行对象回收状态的跟踪,而无法用于获取对象的引用。

那MySQL是怎样执行最终的IO资源回收的呢,是使用了定时线程还是异步守护线程?
是使用异步守护线程处理的
在这里插入图片描述
在这里插入图片描述
所以NonRegisteringDriver的静态成员变量:connectionPhantomRefs, 有几万个对象, 说明了在这段时间积累了大量的数据库连接connection实例进入以下生命周期:
创建 --> 闲置 —> 回收;

我们再回头看看我们的数据库连接池的配置是怎么样的

 maxPoolSize: 80   同事写的,根本就没有这个属性-_-   所以最大默认连接数是10 maximumPoolSize控制最大连接数,默认为10minIdle:   2 这个属性也写错了-_-   minimumIdle控制最小连接数,默认等同于maximumPoolSize,10。idleTimeout: 600000  连接空闲时间超过idleTimeout 10min后连接被抛弃 此设置仅适用于minimumIdle 定义为小于maximumPoolSize 的情况maxLifetime: 1800000 连接生存时间超过 maxLifetime 30分钟后,连接会被抛弃.

根据上述配置可知,每隔 30 min,就会重新创建一批连接实例放入内存中,而每次新建一个数据库连接,都会把该连接放入connectionPhantomRefs集合中。

因为连接资源一般存活时间比较久,经过多次Young GC,一般都能存活到老年代。如果这个数据库连接对象本身在老年代,connectionPhantomRefs中的元素就会一直堆积,直到下次 full gc。同时如果等到full gc 的时候connectionPhantomRefs集合的元素非常多,full gc也会非常耗时。

问题找到了,哪解决方案就呼之欲出了,见下面

3.解决方案

3.1 调整HikariCP配置

HikariCP默认配置(参考:https://github.com/brettwooldridge/HikariCP)
maximumPoolSize

This property controls the maximum size that the pool is allowed to reach, including both idle and in-use connections. Basically this value will determine the maximum number of actual connections to the database backend. A reasonable value for this is best determined by your execution environment. When the pool reaches this size, and no idle connections are available, calls to getConnection() will block for up to connectionTimeout milliseconds before timing out. Please read about pool sizing. Default: 10Default: 10

minimumIdle

This property controls the minimum number of idle connections that HikariCP tries to maintain in the pool. If the idle connections dip below this value and total connections in the pool are less than maximumPoolSize, HikariCP will make a best effort to add additional connections quickly and efficiently. However, for maximum performance and responsiveness to spike demands, we recommend not setting this value and instead allowing HikariCP to act as a fixed size connection pool. Default: same as maximumPoolSizeDefault: same as maximumPoolSize

maxLifetime

This property controls the maximum lifetime of a connection in the pool. An in-use connection will never be retired, only when it is closed will it then be removed. On a connection-by-connection basis, minor negative attenuation is applied to avoid mass-extinction in the pool. We strongly recommend setting this value, and it should be several seconds shorter than any database or infrastructure imposed connection time limit. A value of 0 indicates no maximum lifetime (infinite lifetime), subject of course to the idleTimeout setting. The minimum allowed value is 30000ms (30 seconds). Default: 1800000 (30 minutes)Default: 1800000 (30 minutes)

idleTimeout

This property controls the maximum amount of time that a connection is allowed to sit idle in the pool. This setting only applies when minimumIdle is defined to be less than maximumPoolSize. Idle connections will not be retired once the pool reaches minimumIdle connections. Whether a connection is retired as idle or not is subject to a maximum variation of +30 seconds, and average variation of +15 seconds. A connection will never be retired as idle before this timeout. A value of 0 means that idle connections are never removed from the pool. The minimum allowed value is 10000ms (10 seconds). Default: 600000 (10 minutes)Default: 600000 (10 minutes)

connectionTimeout

This property controls the maximum number of milliseconds that a client (that's you) will wait for a connection from the pool. If this time is exceeded without a connection becoming available, a SQLException will be thrown. Lowest acceptable connection timeout is 250 ms. Default: 30000 (30 seconds)Default: 30000 (30 seconds)

Hikari 推荐 maxLifetime 设置为比数据库的 wait_timeout 时间少 30s 到 1min。如果你使用的是 mysql 数据库,可以使用 show global variables like ‘%timeout%’; 查看 wait_timeout,默认为 8 小时
注意有些公司使用的代理连接,具体wait_timeout可以咨询自己公司的运维组。

3.2 调整mysql-connector-java

升级MySQL jdbc driver到8.0.30,开启disableAbandonedConnectionCleanup
Oracle应该是接收了大量开发人员的反馈,在高版本中已经可以通过配置选择性关闭此功能。 mysql-connector-java 版本(8.0.22+)的代码对数据库连接的虚引用有新的处理方式,其增加了开关,可以手动关闭此功能。

其版本8.0.22介绍增加此参数即是为了解决JVM 虚引用相关问题,但是默认是未启用,需要手动开启:

https://dev.mysql.com/doc/relnotes/connector-j/8.0/en/news-8-0-22.htmlWhen using Connector/J, the AbandonedConnectionCleanupThread thread can now be disabled completely by setting the new system property com.mysql.cj.disableAbandonedConnectionCleanup to true when configuring the JVM. The feature is for well-behaving applications that always close all connections they create. Thanks to Andrey Turbanov for contributing to the new feature. (Bug #30304764, Bug #96870)

​ 有了这个配置,就可以在启动参数上设置属性:

java -jar app.jar -Dcom.mysql.cj.disableAbandonedConnectionCleanup=true

或者在代码里设置属性:

System.setProperty(PropertyDefinitions.SYSP_disableAbandonedConnectionCleanup,"true");

com.mysql.cj.disableAbandonedConnectionCleanup=true 时,生成数据库连接时就不会生成虚引用.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/795560.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[StartingPoint][Tier1]Ignition

Task 1 Which service version is found to be running on port 80? (发现哪个服务版本在端口 80 上运行&#xff1f;) $ nmap -sV -Pn 10.129.1.27 -p 80 nginx 1.14.2 Task 2 What is the 3-digit HTTP status code returned when you visit http://{machine IP}/? (访…

Centos7下docker安装jenkins

个人记录 安装Docker与Docker-compose Centos7安装Docker与Docker-compose【图文教程】 配置docker-compose.yml vim docker-compose.yml按i进行编辑模式&#xff0c;粘贴如下内容 version: 3 services:jenkins:image: jenkinsci/blueoceancontainer_name: jenkinsrestart…

剑指Offer题目笔记26(动态规划的基础知识)

面试题88&#xff1a; 问题&#xff1a; ​ 一个数组cost的所有数字都是正数&#xff0c;它的第i个数字表示在一个楼梯的第i级台阶往上爬的成本&#xff0c;在支付了成本cost[i]之后可以从第i级台阶往上爬1级或2级。请计算爬上该楼梯的最少成本。 解决方案一&#xff1a;&…

Java | Leetcode Java题解之第9题回文数

题目&#xff1a; 题解&#xff1a; class Solution {public boolean isPalindrome(int x) {// 特殊情况&#xff1a;// 如上所述&#xff0c;当 x < 0 时&#xff0c;x 不是回文数。// 同样地&#xff0c;如果数字的最后一位是 0&#xff0c;为了使该数字为回文&#xff0…

Linux:数据链路层

文章目录 路由表数据链路层分片mac帧报头ARP协议ARP的周边话题 路由表 当主机a想要发送消息到主机b&#xff0c;这一整个过程中&#xff0c;数据报文在进行传输的过程实际上是一跳一跳的过去的&#xff0c;而报文可能会经过公网进行传递&#xff0c;本质上这些网络都是靠对应的…

【python毕业设计】基于Python的医院信息管理系统的设计与实现(源码+数据库+毕业论文)【独一无二】

&#x1f449;博__主&#x1f448;&#xff1a;米码收割机 &#x1f449;技__能&#x1f448;&#xff1a;C/Python语言 &#x1f449;公众号&#x1f448;&#xff1a;测试开发自动化【获取源码商业合作】 &#x1f449;荣__誉&#x1f448;&#xff1a;阿里云博客专家博主、5…

Google视觉机器人超级汇总:从RT、RT-2到AutoRT、SARA-RT、RT-Trajectory

前言 随着对视觉语言机器人研究的深入&#xff0c;发现Google的工作很值得深挖&#xff0c;比如RT-2 ​想到很多工作都是站在Google的肩上做产品和应用&#xff0c;​Google真是科技进步的核心推动力&#xff0c;做了大量大模型的基础设施&#xff0c;服 故有了本文&#xf…

【前端Vue】Vue0基础完整教程第5篇:vue指令(下),成绩案例【附代码文档】

Vue从0基础到大神学习完整教程完整教程&#xff08;附代码资料&#xff09;主要内容讲述&#xff1a;vue基本概念&#xff0c;vue-cli的使用&#xff0c;vue的插值表达式&#xff0c;{{ gaga }}&#xff0c;{{ if (obj.age > 18 ) { } }}&#xff0c;vue指令&#xff0c;综合…

更高效、更简洁的 SQL 语句编写丨DolphinDB 基于宏变量的元编程模式详解

元编程&#xff08;Metaprogramming&#xff09;指在程序运行时操作或者创建程序的一种编程技术&#xff0c;简而言之就是使用代码编写代码。通过元编程将原本静态的代码通过动态的脚本生成&#xff0c;使程序员可以创建更加灵活的代码以提升编程效率。 在 DolphinDB 中&#…

【Python系列】将生成的 JSON 数据写入 JSON 文件

&#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:kwan 的首页,持续学…

面试经典-Spring篇

1、解释Spring框架中bean的生命周期 实例化 通过反射去推断构造函数进行实例化 实例工厂、静态工厂 属性赋值 解析自动装配&#xff08;byname、bytype、 constractor、 Autowired&#xff09; 循环依赖 初始化 调用XXXAware回调方法&#xff08;BeanNameAware、BeanFactoryAw…

移动Web学习05-移动端适配Less预处理器

7、移动端适配 7.1、什么是适配&#xff1f; 简单理解就是、同一个网页&#xff0c;在不同屏幕分辨率的设备下、显示还是一样的&#xff0c;你可以理解为、网页当中的图片&#xff0c;盒子之间的距离、文字的大小、随着屏幕分辨率的变化而变化 前面我们学习了flex布局的方式…

Yalmip使用教程(7)-求解器的参数设置

博客中所有内容均来源于自己学习过程中积累的经验以及对yalmip官方文档的翻译&#xff1a;https://yalmip.github.io/tutorials/ 这篇博客将详细介绍yalmip工具箱中常用的求解器设置选项。 1.求解器的基本设置 使用sdpsettings函数可以对求解的相关参数进行设置。最常用的设置…

Java学习day6-集合

集合指一组用于储存和操作数据的类和接口&#xff0c;提供各种数据结构和算法&#xff0c;以在程序中高效地管理和操作数据 特点&#xff1a;与数组相比&#xff0c;集合可以自动扩容&#xff0c;只需向其中添加元素即可&#xff08;与Cvector类似&#xff09;&#xff1b;数组…

软考之零碎片段记录(七)+复习巩固(二)

一、上新 1. 有向图 从顶点A到顶点B的边&#xff0c;不等于从B到A的边。 2. 广度优先 遍历开始节点&#xff08;第一层&#xff09;的邻节点&#xff08;从左至右顺序&#xff09;&#xff0c;邻接点设为第二层根据1中遍历邻接点从左往右的顺序遍历。 bilibili视频《广度优…

《C++程序设计》阅读笔记【5-引用】

&#x1f308;个人主页&#xff1a;godspeed_lucip &#x1f525; 系列专栏&#xff1a;《C程序设计》阅读笔记 本文对应的PDF源文件请关注微信公众号程序员刘同学&#xff0c;回复C程序设计获取下载链接。 1 引用1.1 概念1.2 和引用相关的操作1.2.1 什么能被引用 1.3 用引用传…

WebKit结构揭秘:探秘网页渲染的魔法之源

一、WebKit之心&#xff1a;渲染引擎的魔力 WebKit的渲染引擎是其核心所在&#xff0c;它犹如一位技艺高超的魔法师&#xff0c;将HTML、CSS和JavaScript的魔法咒语转化为绚丽的网页画面。它解析代码&#xff0c;绘制页面&#xff0c;让网页内容跃然屏上&#xff0c;展现出无尽…

ROS 2边学边练(15)-- 写一个简单的服务(C++)

前言 此篇我们即将编写一个简单的服务&#xff08;service&#xff09;通信例子&#xff0c;客户端节点向服务端节点发出请求&#xff08;.srv文件中规定了通信的数据结构格式&#xff09;&#xff0c;服务端节点收到请求后将结果回复给客户端节点&#xff0c;一问一答&#xf…

Unity类银河恶魔城学习记录12-4 p126 Item Tooltip源代码

Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释&#xff0c;可供学习Alex教程的人参考 此代码仅为较上一P有所改变的代码 【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili UI.cs using System.Collections; using System.Collections.Generic; usi…

【面经】interrupt()、interrupted()和isInterrupted()的区别与使用

&#x1f4dd;个人主页&#xff1a;五敷有你 &#x1f525;系列专栏&#xff1a;面经 ⛺️稳中求进&#xff0c;晒太阳 interrupt方法 如果打断线程正在sleep&#xff0c;wait&#xff0c;join会导致被打断的线程抛出InterruptedException&#xff0c;并清除打断标记。如…