这段代码哪里内存泄漏?该函数执行数百万次,占用大量内存,执行240万次后出现内存不足异常。
public static void saveCall(Call call) {
conn = getInstance();
if (conn != null) {
try {
calendar.setTime(call.getDate());
String sql = "INSERT INTO Calls(id, datetime, duration, customer_phone_id, partner_phone_id) "
+ "VALUES(null, ?, ?, ?, ?)";
PreparedStatement preparedStatement = conn
.prepareStatement(sql);
preparedStatement.setString(1, dateFormat.format(calendar.getTime()));
preparedStatement.setLong(2, call.getDuration());
preparedStatement.setLong(3, call.getPhone().getPhoneNumber());
preparedStatement.setLong(4, call.getPhonePartner()
.getPhoneNumber());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
最佳答案
如果您的程序大量使用内存,则可能没有泄漏,而只是垃圾收集器问题。 IE,你的垃圾来得太晚了,无法释放一些空间来创建新对象。
从这里开始,您可能希望在运行查询时分析代码(任何 jdk 提供的 VisualVM 或 jconsole)。 您将看到内存空间是如何填充的(垃圾行为和对象大小)。
然后,如果需要,您将需要配置 jvm 垃圾收集 详细的文档在这里:http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html 如果您分享您的内存配置文件,我们可能会帮助您配置它。
编辑:存在内存泄漏,我错了;-)
关于Java JDBC : MySQL write Out of Memory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16639529/