In Java when doing some operation on Mat (Background Subtraction) the memory is dramatically using by JVM
I am wondering that what is the reason for dramatical memory using Java program with OpenCV, If only read and show the image, then no problem, but when I want to perform so operation, such as
Background Subtraction -> cleaning operation (`OPEN`, `CLOSE`, etc) -> find counters -> draw contours
I tried my best to write the most optimized code (avoiding newing Mat in every loop, trying to reuse the previous object etc), so for example, if I have 8GB RAM
, then in every second the memory used about 40Mb, then in few minutes the RAM
become 100%.
My question is: what is wrong whit OpenCV
in Java
, Or what should I do with Mats
objects? Does Mat
object need to be released after every loop? Or I must not use the global Mat
object in every loop, in this case, I must new a Mat
in every loop?
OpenCV version is: 3.331
show your code, please.
The code is long enough to put here, but the routine I have already mentioned above. OK, I think the problem is with Mat object must be released at the end of work done, the Java JVM not (on time) collect the garbage which produces from OpenCV DLL, so it must be released manually. The other way is to call
System.gc()
System.gc() won't do, it never sees memory allocated from c++. so yes, you'll have to manually release() every Mat, you created with new()
But it really do :) I have tested if I did not release the mat but use
System.gc()
it works great, I use the latest version of JVM, but Mat which newed, cloned, submated, all must be released.ok, if you call it manually ... just saying, that the (java) Mat objects are so small, that it'll never trigger automatically.