Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MySQL memory leak

 
Mike Southgate
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I originally posted an eraly version of this question in the java intermediate forum, but investigation indicates its in the jdbc...

I'm getting a java.lang.OutOfMemoryError after calling this method around 4000 times:


As a test I created a loop that went 10000 times and gradually added lines from the above method to see when it blows up. As it turns out, the offending line is : ps.executeUpdate();

I'm using mysql. Any suggestions on how to proceed. Would unloading and reloading the mysql driver help?

Has anyone else out there used mysql with thousands of inserts?

ms
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wild guess, maybe make the PreparedStatement a class level variable so only one instance is made.

Also move the ps.close() to a finally block. (but if you haven't been seeing any exceptions this probabaly isn't a problem).

On second thought maybe the PS isn't a problem but it looks like you are getting a new connection each time and not closing it. Just guesses, I'm not a JDBC guru by any means.
[ March 09, 2005: Message edited by: Steven Bell ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,
A few comments:
1) Try executing the loop with a different prepared statement. For example, try with one that just does a select. This will provide more clues as to the problem.
2) If you are calling this alot of times in production, consider not making it synchronized or executing multiple inserts in a batch.
3) The "ps = null" statement doesn't do anything as the ps variable is about to go out of scope anyway. This isn't the problem, but it's good to know.
 
Mike Southgate
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeanne Boyarsky:
Mike,
A few comments:
1) Try executing the loop with a different prepared statement. For example, try with one that just does a select. This will provide more clues as to the problem.
2) If you are calling this alot of times in production, consider not making it synchronized or executing multiple inserts in a batch.
3) The "ps = null" statement doesn't do anything as the ps variable is about to go out of scope anyway. This isn't the problem, but it's good to know.


Thanks for the input. Re # 3, that is actually left over code from when I thought the issue might be garbage collection and was trying to force earlier cleanup.
I made it synchronized to allow for multi-threading, though that isn't an issue yet.
Batching the inserts is a good idea, that might at least reduce the issue.
I'll also try using selects instead.
One last thing is this used to work OK before I started using the JRE that came with J2EE. I think I'll give 1.5 a shot and see what happens.

ms
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic