这几天苦恼于这个问题,用jprofiler测试返现执行数据库操作就存在内存泄露,不执行数据库操作(但是会取数据库连接和释放连接)就不会发生内存泄露。数据库执行的地方如下,如果我将ps.executeUpdate();注释掉内存一直很平稳,但打开注释,内存一直往上涨,最后就out of memory了。其中有一直上涨的几项是Hashmap$Entry和long[]。 PreparedStatement ps = null;
Connection conn = DBConnectionManager.getInstance().getConnection(); try {
ps = conn.prepareStatement(sql);
ps.setInt(1,2);
ps.executeUpdate();
} catch (Exception e) {
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
DBConnectionManager.getInstance().freeConnection(conn);
}
} catch (SQLException sqlEx) { }
}请高手相救,谢谢。
Connection conn = DBConnectionManager.getInstance().getConnection(); try {
ps = conn.prepareStatement(sql);
ps.setInt(1,2);
ps.executeUpdate();
} catch (Exception e) {
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
DBConnectionManager.getInstance().freeConnection(conn);
}
} catch (SQLException sqlEx) { }
}请高手相救,谢谢。
请有经验的高手帮忙,非常感谢
Connection con = null;
if (freeConnections.size() > 0) {
con = (Connection)freeConnections.get(freeConnections.size() - 1);
freeConnections.remove(freeConnections.get(freeConnections.size() - 1)); } else { con = newConnection();
} return con;
} public synchronized Connection getConnection() {
Connection con = null;
while ((con = getAvailableConnection()) == null) {
try {
wait(500);
} catch (Exception e) {
e.printStackTrace();
return null;
} return con;
}
public synchronized void freeConnection(Connection con) {
freeConnections.addElement(con);
notifyAll();
}
public synchronized Connection getConnection() {
Connection con = null;
while ((con = getAvailableConnection()) == null) {
try {
wait(500);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}return con;
}public synchronized Connection getAvailableConnection() {
Connection con = null;
if (freeConnections.size() > 0) {
con = (Connection)freeConnections.get(freeConnections.size() - 1);
freeConnections.remove(freeConnections.get(freeConnections.size() - 1));} else {con = newConnection();
}return con;
}
public synchronized void freeConnection(Connection con) {
freeConnections.addElement(con);
notifyAll();
}
还是有问题啊,其他的代码都没有问题,因为其他所有的代码都注释的话还是会出现这个问题
Connection con = null;
while ((con = getAvailableConnection()) == null) {
try {
wait(500);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}return con;
}
估计这里的问题了。
死也取不到连接,while一直在跑,就outofmemory咯 。你用dbcp,getConnection之类的用它的还是自己的?如果自己的,问题也在这里。
如果它的,你的代码调用有问题。代码对,所有的rs、ps、con都及时释放,dbcp不会像你那样暴涨的。
不是追求,你在问题里面说了:
一直上涨的几项是Hashmap$Entry和long[]。哪里用到了Hashmap?我看了,貌似你的Connection都放在这里面。
从日志看到40足够用,没有new connection的日志在
所以connection这块没有任何问题
就因为如此我才上来问,否则这么简单的问题我早就自己解决了。谢谢