out of memory after executeBatch

88 views
Skip to first unread message

mche...@gmail.com

unread,
Nov 21, 2023, 5:41:30 AM11/21/23
to H2 Database
Dear All
    I keep clicking the data to h2 by stmt.addBatch(), after i called executeBatch, memory is not free, so finally out of memory, may i know why please?


@Override
public void run() {
while (!stopped) {
synchronized (sharedLock) {
try {
// System.out.println("data.size() =" + data.size());
for (int z = 0; z < data.size() && z < 100000; z++) {
Object[] temp;
synchronized (data) {
temp = data.pollLast();
}
int x = 1;
stmt.setLong(x++, (Long) temp[0]);
stmt.setString(x++, (String) temp[1]);
stmt.setString(x++, (String) temp[2]);
stmt.setString(x++, (String) temp[3]);
stmt.setInt(x++, (int) temp[4]);
for (int tempX = 0; tempX < registers.size(); tempX++) {
stmt.setLong(x, ((BigInteger) temp[x - 1]).longValue());
x++;
}
stmt.addBatch();
}
if (data.size() % 100000 == 0 || data.size() < 100000) {
stmt.executeBatch();
conn.commit();
}
} catch (SQLException ex) {
Logger.getLogger(H2Thread.class.getName()).log(Level.SEVERE, null, ex);
}
}
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
Logger.getLogger(H2Thread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

thanks

Andrei Tokar

unread,
Nov 21, 2023, 10:36:39 PM11/21/23
to H2 Database
Hi Peter,
It is really hard to tell exactly what the problem with your code is, because it is not a complete one, but in case of OOM some "irrelevant" details may be a culprit.
Nevertheless, it looks like "data" is some sort of a queue, continuously populated by another thread, or just a big chunk of preropulated data?
In both cases your condition for batch commit (data.size() % 100000 == 0 || data.size() < 100000) looks suspicious. Let's assume prepopulated data of size
100000 * 12345 +1
You may end up committing batch of size 1.2 billion+, wchich would totally explain OOM..

mche...@gmail.com

unread,
Dec 3, 2023, 10:58:42 AM12/3/23
to H2 Database
I figured it out. Java jdbc need some time to flush the cache after all batch are executed.
Reply all
Reply to author
Forward
0 new messages