数据库Java deque/prepared语句内存泄漏
下面的一段代码会产生内存泄漏,知道是哪一部分吗
(一)
private Deque<Snapshot> snapshots = new LinkedList<Snapshot>();
Iterator<Snapshot> i = world.getSnapshots().descendingIterator();
while (i.hasNext()) {
Snapshot s = i.next();
if (curTime - s.getTimestamp() > 60000) {
i.remove();
} else {
break;
}
}
(二)
public static void initilizePreparedStatements() {
try {
insertNewReportRow = Instance.getWorld().getDB().getConnection().prepareStatement("INSERT INTO `rsca2_reports` (`from`, `about`, `time`, `reason`, `snapshot_from`,`snapshot_about`,`chatlogs`, `from_x`, `from_y`, `about_x`, `about_y`) VALUES(?,?,?,?,?,?,?,?,?,?,?)");
} catch (SQLException e) {
e.printStackTrace();
Logger.error(e);
}
}
public synchronized static void submitReport() {
/*removed*/
try {
insertNewReportRow.setLong(1, from);
insertNewReportRow.setLong(2, about);
insertNewReportRow.setLong(3, time);
insertNewReportRow.setInt(4, reason);
insertNewReportRow.setString(5, snapshot_from);
insertNewReportRow.setString(6, snapshot_about);
insertNewReportRow.setString(7, chatlog);
insertNewReportRow.setInt(8, f.getX());
insertNewReportRow.setInt(9, f.getY());
insertNewReportRow.setInt(10, a.getX());
insertNewReportRow.setInt(11, a.getY());
insertNewReportRow.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
Logger.error(e);
}
}
# 1 楼答案
根据实现的不同,迭代器中的
breaking
可能会导致迭代器自身无法完成,并阻止自身释放绑定的资源,从而导致内存泄漏。也有可能你从来没有清洁过你的头发,这会导致头发尺寸随着时间的推移呈线性增长# 2 楼答案
我的猜测是
Instance.getWorld().getDB().getConnection()
在这里您可以获得一个连接,并且只存储对它创建的准备好的语句的引用。 这意味着,当代码使用准备好的语句完成时,您不会释放连接,并且(假设它来自连接池)连接池不会回收该连接,但它会在其映射中保留对该连接的引用