简单代码中的内存泄漏?在任何地方都找不到答案

时间:2019-03-28 15:33:38

标签: java

此代码逐渐消耗大约130 MB的内存(由于依赖关系),并且在我不得不终止并重新启动之前(由于服务器内存不足),一直爬升至800+ MB。

它与OpenJDK 11一起运行。我在Java 8服务器上运行此代码的较旧版本,该服务器的内存使用率保持稳定且从未增加。所以我不确定是否与新的JDK有关?

我在这里修改了很多代码以确保它尽可能简单-但仍然存在问题。

基本要点-它是否每隔几秒钟就查询数据库中是否有未结发票。但是,由于没有待处理的发票(日志也证明了这一点),因此它永远不会进入复杂的代码位置,并且只会每隔几秒钟重复一次。

public static void main(String[] args) {
    ...

    final int interval = Constants.INTERVAL;
    QuickBooksInvoices qbInvoices = new QuickBooksInvoices(filename);
    qbInvoices.testConnection();

    log.log(Level.INFO, "Checking invoices with an interval of " + interval + " seconds...");

    while (isRunning == true) {
        qbInvoices.process();

        try {
            Thread.sleep(interval * 1000);
        } catch (InterruptedException e) {

        }
    }
}

public void process() {
    errorBuffer.clear();  // These are array lists
    successBuffer.clear(); // These are array lists

    try (Connection conn = DriverManager.getConnection(dbURI, dbUser, dbPassword)) {
        ArrayList<com.xxx.quickbooks.model.wdg.Invoice> a = getInvoices(conn);
        OAuthToken token = null;

        if (a.size() > 0) {
            // Never gets here - no results
        }

        for (com.xxx.quickbooks.model.wdg.Invoice invoice : a) {
            // Never gets here - no results
        }
    } catch (Exception e) {        
        writeLog(Level.ERROR, ExceptionUtils.getStackTrace(e));
    }

}

private ArrayList<com.xxx.quickbooks.model.wdg.Invoice> getInvoices(Connection conn) {
    ArrayList<com.xxx.quickbooks.model.wdg.Invoice> invoices = new ArrayList<com.xxx.quickbooks.model.wdg.Invoice>();  

    String sql = 
        "select " +
        "id," +
        "type," +
        "status," +
        "business_partner_id," +
        "invoice_number," +
        "total," +
        "nrc," +
        "szrc," +
        "trans_ts," +
        "warehouse_id," +
        "due_date," +
        "ref_number," +
        "payment_type " +
        "FROM dv_invoice " +
        "WHERE exported_ts is NULL AND exported_msg is NULL ; ";

    try (
        PreparedStatement stmt = conn.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
    ) {
        while (rs.next()) {
            // Never gets here - no results
        }
    } catch (SQLException e) {
        writeLog(Level.ERROR, ExceptionUtils.getStackTrace(e));
    }
    return invoices;
}

1 个答案:

答案 0 :(得分:0)

驱动程序可能会产生内存泄漏。我在统一SQL数据库上遇到此问题。将驱动程序更新到最新版本。

See this in postgres

相关问题