I'm using OpenCV's(3.4.1) JNI wrapper as port for to use OpenCV in Java, and i need handle a lots jpeg images.
It's fine and no memory leak if handle in single thread. But huge memory leak when using multiple threads.
Can anyone help me fix this?
My code:
int nThreads = 128;
ExecutorService pool = Executors.newFixedThreadPool(nThreads);
while (true) {
CountDownLatch ready = new CountDownLatch(nThreads);
CountDownLatch start = new CountDownLatch(1);
CountDownLatch end = new CountDownLatch(nThreads);
for (int i = 0; i < nThreads; i++) {
int index = i;
pool.execute(() -> {
ready.countDown();
try {
start.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
Mat mat = Imgcodecs.imread(files[index].getAbsolutePath(), Imgcodecs.IMREAD_COLOR);
mat.free(); // same as release() and n_delete()
end.countDown();
});
}
ready.await();
start.countDown();
end.await();
System.out.println("Sleep");
Thread.sleep(5000);
}