opencv: memory leak with videocapture, memory goes up with the function of read(image)

Hello! I am using opencv 3.0 beta with visual studio 2013 under windows 7 and 64bit. I downloaded opencv3.0 beta for windows from http://opencv.org/downloads.html. The following is the code I am using to capture the video from rtsp stream and then save some frames every several minutes. It has no problem to capture and read the stream. But I found, the function “read(InputArrray Image)” cause the memory leak. The memory goes up slowly, about one MB for two or three hours. But it keep going up. I am using it for severy months continually. so it is seriouly problem for me. The following is my code for test. Please help me find out where is my problem with these code. Thanks a lot! int main() { string fileNameSave; string URLName;

BOOL stop = FALSE;
BOOL saveFrame = TRUE;
BOOL got_frame = FALSE;
string fileSave = "saveFrame.jpg";
URLName = "rtsp://username:password@IP adress:554/h264/ch1/main/av_stream";
VideoCapture capture;
Mat frame;
capture.open(URLName);
if (!capture.isOpened()){
    cout << " channel 1st has error " << endl;
}
while (!stop){  
    got_frame = capture.read(frame); //here the memory keep goes up
    //save the frame every minute locally
    if (saveFrame == TRUE){
        if (got_frame){
            imwrite(fileSave, frame);
        }
    }
}
capture.release();
return 0;

}

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 40 (8 by maintainers)

Most upvoted comments

2020 and the problem still persists

Growing “cache” memory is not a sign of a memory leak (https://www.linuxatemyram.com/).

Using opencv-python 4.1.2.30 and still facing the issue. Every call to VideoCapture is increasing memory by 20MB.

This is a shame and disaster that even after 7 years this bug has not been fixed and nobody has bothered to provide a solution.

8 years later, in Jun 2023, version 4.7.0, the problem is still persist. Thank you for your hard work.

8 years later, in Mar 2023, version 4.5.0, the problem is still persist. Thank you for your hard work.

I think I found the problem with the gstreamer backend. If your application is actual a little bit slower than your configured fps you got a memory leak. If you look at the cap_gstreamer.cpp file at line 768 gst_app_sink_set_drop is why ever out commented. The internal buffer is set to one but you must also set gst_app_sink_set_drop true to delete the internal queue of gstreamer or comment out bool streamer.

If you set this function to true all looks fine. But this should only be done if you stream something and you want only the most updated frame because if you do not poll accurate the stream some frames will be skipped!

You can simply test this problem if you set your stream for example to 25 fps and call the read method every one second.

The whole problem is described here: https://gstreamer.freedesktop.org/data/doc/gstreamer/head/gst-plugins-base-libs/html/gst-plugins-base-libs-appsink.html.

Please provide info about your OpenCV build: cv::getBuildInformation() (“Video I/O” section).

You could run your application via valgrind. It is able to show more information about memory leaks. Also remove imshow / waitKey calls - they are not related to mentioned problem. Provide minimal reproducer.

The information about Video I/O got by cv::getBuildInformation():

Video I/O:
    DC1394:                      YES (ver 2.2.4)
    FFMPEG:                      YES
      avcodec:                   YES (ver 56.60.100)
      avformat:                  YES (ver 56.40.101)
      avutil:                    YES (ver 54.31.100)
      swscale:                   YES (ver 3.1.101)
      avresample:                NO
    GStreamer:                   
      base:                      YES (ver 1.8.3)
      video:                     YES (ver 1.8.3)
      app:                       YES (ver 1.8.3)
      riff:                      YES (ver 1.8.3)
      pbutils:                   YES (ver 1.8.3)
    libv4l/libv4l2:              NO
    v4l/v4l2:                    linux/videodev2.h
    gPhoto2:                     NO

The summary of valgrind analysis :

==28587== HEAP SUMMARY:
==28587==     in use at exit: 1,258,916,694 bytes in 28,055 blocks
==28587==   total heap usage: 75,814 allocs, 47,759 frees, 1,365,341,099 bytes all$
==28587==
==28587== LEAK SUMMARY:
==28587==    definitely lost: 64,498 bytes in 205 blocks
==28587==    indirectly lost: 273,806,623 bytes in 479 blocks
==28587==      possibly lost: 982,919,030 bytes in 329 blocks
==28587==    still reachable: 2,057,887 bytes in 26,720 blocks
==28587==                       of which reachable via heuristic:
==28587==                         length64           : 8,032 bytes in 190 blocks
==28587==                         newarray           : 1,680 bytes in 25 blocks
==28587==         suppressed: 0 bytes in 0 blocks
==28587== Reachable blocks (those to which a pointer was found) are not shown.
==28587== To see them, rerun with: --leak-check=full --show-leak-kinds=all

The complete valgrind log file is available at https://github.com/trocolit2/opencv_workshop/blob/master/samples/valgrind.txt