以下代码片段是否会泄漏内存
一个
BufferedWriter logoutput;
FileWriter fstream = null;
try {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FILE_FORMAT_NOW);
fstream = new FileWriter("..\\GetInfoLogs\\" + sdf.format(cal.getTime()) + ".log", true);
logoutput = new BufferedWriter(fstream);
logoutput.write(statement);
// Missing fstream.close();
logoutput.close();
}
} catch (IOException e) {
System.err.println("Unable to write to file");
}
乙
String info[] = {"", ""};
try {
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("select ....");
boolean hasRows = rset.next();
if (!hasRows) {
stmt.close();
return info;
} else {
info[0] = rset.getString(1);
info[1] = rset.getString(2);
}
// MISSING rset.close();
stmt.close();
} catch (SQLException e) {
logTransaction(service, "error at getPFpercentage: " + e.getMessage() + " ");
}