Java MySQL JDBC 内存泄漏 [英] Java MySQL JDBC Memory Leak

查看:23
本文介绍了Java MySQL JDBC 内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我的这个程序有很多(~300)个线程,每个线程都与一个中央数据库进行通信.我创建了一个到 DB 的全局连接,然后每个线程开始自己的业务,创建语句并执行它们.

Ok, so I have this program with many (~300) threads, each of which communicates with a central database. I create a global connection to the DB, and then each thread goes about its business creating statements and executing them.

在此过程中,我遇到了大量内存泄漏.分析堆转储后,我看到 com.mysql.jdbc.JDBC4Connection 对象为 70 MB,因为它在openStatements"(哈希映射)中有 800,000 个项目.某处它没有正确关闭我创建的语句,但我终生无法弄清楚在哪里(每次我打开一个,我也关闭它).知道为什么会发生这种情况吗?

Somewhere along the way, I have a massive memory leak. After analyzing the heap dump, I see that the com.mysql.jdbc.JDBC4Connection object is 70 MB, because it has 800,000 items in "openStatements" (a hash map). Somewhere it's not properly closing the statements that I create, but I cannot for the life of me figure out where (every single time I open one, I close it as well). Any ideas why this might be occurring?

推荐答案

你知道除非 MySQL 这么说,否则 JDBC 连接不是线程安全的.您不能跨线程共享它们,除非您使用连接池.此外,正如所指出的,您应该尝试/最终保证所有语句、结果集和连接都已关闭.

You know unless MySQL says so, JDBC Connections are NOT thread safe. You CANNOT share them across threads, unless you use a connection pool. In addition as pointed out you should be try/finally guaranteeing all statements, result sets, and connections are closed.

这篇关于Java MySQL JDBC 内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆