以下代码片段会泄漏内存吗

一种

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() + " ");

    }

最佳答案

我建议使用YourKit Java Profiler,因为它非常直观且易于使用。

在本地启动您的应用程序,将Profiler连接到它并执行一些应用程序用例。

07-24 19:53