Where is the memory leak in this code? This function is executed millions of times with an extensive usage of memory, causing an out of memory exception after 2.4million execusions.
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();
}
}
}
PreparedStatements. Also, Could you please show us yourgetInstance?getInstancemethod at all. Are you sure that your connection will be closed?getInstance()uses the same connection over and over again, it does not matter. Lets assume that he has really fixed the problem ... rather than attempting to redesign his entire application without seeing the source code :-)getInstanceis able to handle situations when connection is no longer valid and 2)there is no need in several connections then i'm ok with it. I just wasn't sure thatPreparedStaments are the only objects to blame.