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)
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 atline 768 gst_app_sink_set_drop
is why ever out commented. The internal buffer is set to one but you must also setgst_app_sink_set_drop
true to delete the internal queue of gstreamer or comment outbool 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()
:The summary of valgrind analysis :
The complete valgrind log file is available at https://github.com/trocolit2/opencv_workshop/blob/master/samples/valgrind.txt