此Java代码中的潜在内存泄漏导致堆错误

时间:2016-08-19 09:37:12

标签: java memory-leaks

我无法找到这段代码长时间运行时产生内存不足堆错误的原因。它会在崩溃前持续运行1-2天。谁能看到任何东西? 我只调用另一个不能保持任何资源打开的类的静态函数,然后调用mysql的东西,但我把它们都关闭了。

注意:某些变量名称已更改。

public class Application {

public static void main(String ...args) throws InterruptedException, IOException {
    Data.connectDb(); //init

try {

    List<Integer> written = new ArrayList<>();
    Connection con = Data.getConnection();
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM table2 LIMIT 500");
    while (rs.next()) {
        Integer mId = rs.getInt("mId");
        written.add(mId);
    }

    Map<Integer,String> bs = new HashMap<>();
    rs = st.executeQuery("SELECT * FROM b");
    while (rs.next()) {
        bs.put(rs.getInt("bId"),rs.getString("name"));
    }
    st.close();
    rs.close();

    while (true) {
        List<String> abers = new ArrayList<>();
        st = con.createStatement();
        rs = st.executeQuery("SELECT * FROM table1 WHERE enabled = 1");
        while (rs.next()) {
            String email = rs.getString("email");
            abers.add(email);
        }
        rs.close();
        st.close();

        ArrayList<Map> abs = Aber.getAbs(false);
        ArrayList<Map> toDispatch = new ArrayList<>();


        arbs.forEach((ab) -> {
            Integer mId = (Integer) ab.get("mId");
            if (!written.contains(mId)) {
                written.add(mId);
                toDispatch.add(arb);
                Pair hLine = (Pair) ab.get("hLine");
                Pair dLine = (Pair) ab.get("dLine");
                Pair aLine = (Pair) ab.get("aLine");
                //todo write it to the database not caring if it's already there as we have a unique constraint
                try (Statement st2 = con.createStatement()) {
                    String sql = "INSERT INTO table1")";
                    st2.executeUpdate(sql);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        });

        if (toDispatch.size() >0) {

            Email email = new Email();

            abes.forEach(arber -> email.addRecipient(aber, aber, Message.RecipientType.BCC));

            String HTML = "<h3>Dear subscribers</h3>";
            email.setTextHTML(HTML);

            new Mailer(
                    new ServerConfig("mail.name.com", 587, "info@mm.com", "mmpass"),
                    TransportStrategy.SMTP_TLS
            ).sendMail(email);

        }

        Thread.sleep(600000);
    }
} catch (SQLException sqle) {
    System.out.println(sqle.getMessage());
}
}
}

0 个答案:

没有答案