gpt4 book ai didi

Java 双端队列/准备语句内存泄漏

转载 作者:行者123 更新时间:2023-12-01 17:40:22 30 4
gpt4 key购买 nike

以下代码片段之一会产生内存泄漏,您知道是哪一部分吗?

1)

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;
}
}

2)

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);
}
}

最佳答案

我的猜测是,您可以在 Instance.getWorld().getDB().getConnection() 中获取连接,并且仅存储对其创建的准备好的语句的引用。这意味着当代码使用准备好的语句完成时,您不会释放连接,并且(假设它来自连接池)连接池不会回收连接,但会在其映射中保留对它的引用。

关于Java 双端队列/准备语句内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1584597/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com