This code gradually consumes memory starting at about 130 MB (due to dependencies) and keeps climbing to 800+ MB before I have to kill it and restart it (as the server runs out of memory).
It's running with OpenJDK 11. I have an older version of this code running on a Java 8 server whose memory usage stays stable and never increases. So I'm not sure if it has to do with the new JDK?
I modified the code here quite a bit to ensure it's as simple as possible - yet still has the problem.
Basic gist - is it queries the database every couple seconds for pending invoices. However, there are no pending invoices (log proves this as well), so it never gets into complex code locations and just continues to repeat every couple seconds.
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;
}