Ask Your Question

Matthias's profile - activity

2020-03-19 00:38:58 -0500 received badge  Popular Question (source)
2014-01-11 11:51:18 -0500 commented question Java: Memory leak from iterating OpenCV frames

First of all, doing it in a loop instead of an iterator won't help too much. I could simply use the second version of the iterator, and just copy the frames away, which I need for later.

And of course, I just posted a minimized example here. The original iterator is way more complex. E.g. it starts a new thread to retreive the pictures simultaneously to processing the old, "interesting" pictures found so far.

2014-01-11 07:02:51 -0500 asked a question Java: Memory leak from iterating OpenCV frames

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. It fills a GByte within seconds. Here is a very simplified version of the iterator, which has this leak:

public final class SimpleIt implements Iterator<Mat> {

    private final VideoCapture capture;
    boolean hasNext;

    public SimpleIt(final VideoCapture capture) {
        this.capture = capture;
        hasNext = capture.grab();

    public boolean hasNext() {
        return hasNext;

    public Mat next() {
        final Mat mat = new Mat();
        hasNext = capture.grab();
        return mat;

When I Iterate over this code using e.g. this loop:

    final VideoCapture vc = new VideoCapture("/path/to/file");
    final SimpleIt it = new SimpleIt(vc);
    while (it.hasNext) {;

memory consumption will increase linear. I see that the problem is the first line in the next()-Method. It always creates a new Mat. But speaking of java alone, this Mat will run out of scope as soon as the statement is over.

I could overcome the problem, by not using a new Mat every time, but overwriting always the same Mat-Object, like this:

    private final VideoCapture capture;
    private final Mat mat = new Mat();
    boolean hasNext;

    public Mat next() {
        hasNext = capture.grab();
        return mat;

But now the last frame which was given by the iterator will always be overwritten by the current frame. Thus, I cannot hold it outside for later use, if I am interested in this single frame. I could copy it, of course, but that would also be expensive.

To illustrate the problem with the last approach, imagin this code using the iterator:

    final VideoCapture vc = new VideoCapture("/path/to/file");
    final SimpleIt it = new SimpleIt(vc);
    int i = 0;
    Mat save = null;
    while (it.hasNext) {
        final Mat next =;
        if (i == 10) {
            save = next;
            Highgui.imwrite("/path/to/10.png", save);
        } else if (i == 30) {
            Highgui.imwrite("/path/to/30.png", save);

With the second version of the iterator, 10.png, and 30.png will be different images. But that's obviously not what was intended.

I assume that the problem is that the garbage collector will not destroy the Mat objects, because it does not recognize the memory consumption, since it is not java heap space. Calling mat.release() in the loop will help, but of course in real code this means I will have no garbage collection for my Mat objects.

Anybody has an idea how to do it?

Edit: I expermiented now a while with it, and came to the following solution:

    int count = 0;

    public Mat next() {
        final Mat result = mat;
        mat = new Mat();
        hasNext = capture.grab();
        if (++count % 200 == 0) {
        return result;

If I call the garbage collector on a regular basis, it will delete all the unreferenced Mat objects, and together with them the RAM allocated from C. Well, that's probably a problem you have to face when mixing ... (more)

2014-01-09 08:00:53 -0500 commented answer Unable to open some avi files in OpenCV-Python -- Why?

I found the solution for me: I set the OPENCV_DIR to the x86 directory of the libs. But in fact I used a x64 java. Changing the OPENCV_DIR solved the problem for me.

2014-01-05 04:49:48 -0500 commented question I cannot build java in 3.0.0

Cool, it worked. Thanks a lot! If this would have been an answer, I would have accepted it. ;-)

2014-01-05 03:17:12 -0500 asked a question I cannot build java in 3.0.0

Hi all,

I try to configure CMake to build OpenCV with java support. Everything is fine, but it will not build java. The configure output even says so:

Checking for Windows Platform SDK
Checking for Visual Studio 2012
Could NOT find PythonInterp (missing:  PYTHON_EXECUTABLE) (Required is at least version "2.7")
Could NOT find PythonInterp (missing:  PYTHON_EXECUTABLE) (Required is at least version "2.6")
Found apache ant 1.9.3: E:/bin/apache-ant-1.9.3/bin/ant.bat

General configuration for OpenCV 3.0.0-dev =====================================
  Version control:     

    Host:                        Windows 6.1 AMD64
    CMake generator:             Visual Studio 12 Win64
    CMake build tool:            C:/PROGRA~2/MSBuild/12.0/Bin/MSBuild.exe
    MSVC:                        1800

    Built as dynamic libs?:      NO
    C++ Compiler:                E:/visualStudio/VC/bin/x86_amd64/cl.exe  (ver 18.0.21005.1)
    C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR /EHa  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /wd4251 /MT /O2 /Ob2 /D NDEBUG  /Zi
    C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR /EHa  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /wd4251 /D_DEBUG /MTd /Zi /Ob0 /Od /RTC1 
    C Compiler:                  E:/visualStudio/VC/bin/x86_amd64/cl.exe
    C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /MT /O2 /Ob2 /D NDEBUG  /Zi
    C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /D_DEBUG /MTd /Zi /Ob0 /Od /RTC1 
    Linker flags (Release):      /machine:x64   /NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:msvcrt.lib /NODEFAULTLIB:msvcrtd.lib /INCREMENTAL:NO  /debug /NODEFAULTLIB:libcmtd.lib
    Linker flags (Debug):        /machine:x64   /NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:msvcrt.lib /NODEFAULTLIB:msvcrtd.lib /debug /INCREMENTAL  /NODEFAULTLIB:libcmt.lib
    Precompiled headers:         YES

  OpenCV modules:
    To be built:                 core flann imgproc highgui features2d calib3d ml objdetect video ocl bioinspired cudaarithm nonfree contrib cudawarping cuda cudafilters cudaimgproc legacy cudabgsegm cudacodec cudafeatures2d cudaoptflow cudastereo optim photo shape softcascade stitching superres ts videostab
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 androidcamera cudalegacy cudev java matlab python viz

  Windows RT support:            NO

    QT:                          NO
    Win32 UI:                    YES
    OpenGL support:              NO
    VTK support:                 NO

  Media I/O: 
    ZLib:                        build (ver 1.2.8)
    JPEG:                        build (ver 90)
    WEBP:                        build (ver 0.3.1)
    PNG:                         build (ver 1.5.12)
    TIFF:                        build (ver 42 - 4.0.2)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 1.7.1)

  Video I/O:
    Video for Windows:           YES
    DC1394 1.x:                  NO
    DC1394 2.x:                  NO
    FFMPEG:                      YES (prebuilt binaries)
      codec:                     YES (ver 55.18.102)
      format:                    YES (ver 55.12.100)
      util:                      YES (ver 52.38.100)
      swscale:                   YES (ver 2.3.100)
      gentoo-style:              YES
    OpenNI:                      NO
    OpenNI PrimeSensor Modules:  NO
    PvAPI:                       NO
    GigEVisionSDK:               NO
    DirectShow:                  YES
    Media Foundation:            NO
    XIMEA:                       NO
    Intel PerC:                  NO

  Other third-party libraries:
    Use IPP:                     NO
    Use Eigen:                   NO
    Use TBB:                     NO
    Use OpenMP:                  NO
    Use GCD                      NO
    Use Concurrency              YES
    Use C=:                      NO
    Use Cuda:                    NO ...
2013-12-21 07:19:28 -0500 answered a question Unable to open some avi files in OpenCV-Python -- Why?

I still cannot answer the question why, but I found a workaround which helps at least for now:

Download mplayer. This package contains also the program mencoder. Now take a video file (in.avi), and run this command:

mencoder.exe in.avi -nosound -ovc raw -vf format=i420 -o out.avi

out.avi will have no sound. If you need it, you can omit -nosound, but you will probably have to give another command line argument.

out.avi will be very large, but at least you can open it with OpenCV on Windows.

2013-12-21 02:51:38 -0500 commented question Unable to open some avi files in OpenCV-Python -- Why?

Unfortuantely I have not an answer here, but it seems that it is not a problem of python. I have the same problem with java, and C++. I can read none of the video files I tried in various formats. The author of this questoin kindly sent me the file julius.avi, and I can read this one. With java, and with C++.

2013-12-21 02:47:35 -0500 answered a question Java: I don't get to work

Can anybody close my quesion, since it is just a duplicate of this one?

My problem has nothing to do with java at all. I asked the author of the linked question for his file julius.avi. I can read this one with java, but no other file.

2013-12-20 11:37:36 -0500 received badge  Supporter (source)
2013-12-20 11:23:24 -0500 commented question Help to Open Video File

I have exactly the same problem, and no clue what causes the problem. Recompiling OpenCV might not be the point, because I didn't compile it in first place. I just used the compiled libs from the homepage.

2013-12-20 11:19:30 -0500 commented question Java: I don't get to work

Ok, after some work I got a C++ program to compile (not so easy, if you never build C++ programs :-). I wrote basically the same program in C++. It also prints false. So, it can probably be ruled out that it is a bug in the java wrapper. Still, I have no idea what exactly the problem is. So, I am grateful for every hint.

2013-12-16 11:03:24 -0500 received badge  Editor (source)
2013-12-16 11:02:36 -0500 asked a question Java: I don't get to work

Hi all,

I try to get the following java code to work:

public static void main(final String[] args) throws IOException {
    final String filename = "E:/tmp/m.avi";
    final VideoCapture vc = new VideoCapture();
    final boolean result =;

Unfortuantely, it prints "false", thus it will not open the file.

The system is a Windows 7. I have no additional codecs installed. The file is definitely at this place. I can watch it with Windows Media Player.

You can find the file attached to this bug report as m.avi.

Thanks for your help!