Memory Leak from iterating Opencv frames

前端 未结 5 2089
囚心锁ツ
囚心锁ツ 2020-12-14 20:20

I am using the java wrapper of OpenCV. I tried to write an Iterator over frames of a film. My problem is that the iterator is a huge memory leak. Here is a very simplified v

5条回答
  •  爱一瞬间的悲伤
    2020-12-14 20:54

    You should really call mat.release().

    I have very similar problem to yours in my application. The frame rate was so high that java heap grew up to total available system memory, which sometimes led to JVM crash. GC was simply too slow, and I didn't have any mechanism to check of available memory and wait if that wasn't sufficient.

    One solution was to decrease the frame rate by simply using Thread.sleep() which of course didn't seem to be acceptable. But it helped GC to do it's job on time.

    Finally using mat.release() fixed the problem.

    You don't have to worry about garbage collection of Mat objects, because this call deallocates only underlying data. Java object wrapper will be disposed by GC when the right time comes.

提交回复
热议问题