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

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

问题描述

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

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天全站免登陆