Java MySQL JDBC Memory Leak

前端 未结 4 1589
死守一世寂寞
死守一世寂寞 2020-12-08 23:13

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 ab

4条回答
  •  萌比男神i
    2020-12-08 23:30

    I had exactly the same problem. I needed to keep 1 connection active for 3 threads and at the same time every thread had to execute a lot of statements (the order of 100k). I was very careful and I closed every statement and every resultset using a try....finally... algorithm. This way, even if the code failed in some way, the statement and the resultset were always closed. After running the code for 8 hours I was suprised to find that the necessary memory went from the initial 35MB to 500MB. I generated a dump of the memory and I analyzed it with Mat Analyzer from Eclipse. It turned out that one com.mysql.jdbc.JDBC4Connection object was taking 445MB of memory keeping alive some openStatements objects wich in turn kept alive aroun 135k hashmap entries, probably from all the resultsets. So it seems that even if you close all you statements and resultsets, if you do not close the connection, it keeps references to them and the GarbageCollector can't free the resources.

    My solution: after a long search I found this statement from the guys at MySQL:

    "A quick test is to add "dontTrackOpenResources=true" to your JDBC URL. If the memory leak goes away, some code path in your application isn't closing statements and result sets."

    Here is the link: http://bugs.mysql.com/bug.php?id=5022. So I tried that and guess what? After 8 hours I was around 40MB of memory required, for the same database operations. Maybe a connection pool would be advisible, but if that's not an option, this is the next best thing I came around.

提交回复
热议问题