Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VideoCapture has Memory leak in opencv4.0.0 #13255

Open
yanchao12122 opened this issue Nov 23, 2018 · 14 comments

Comments

7 participants
@yanchao12122
Copy link

commented Nov 23, 2018

  • OpenCV => 4.0.0
  • Operating System / Platform => Windows 64 Bit
  • Compiler => Visual Studio 2015

In the following code,
the memory will increase slowly along with the time elapsed.

int main()
{
	cv::VideoCapture cap;
	cv::Mat frame;
	while (true) {
		cap.open(0);
		cap >> frame;
		cv::imshow("vidoe", frame);
		cv::waitKey(10);
		cap.release();
	}
	return 0;
}
@peters

This comment has been minimized.

Copy link
Contributor

commented Nov 26, 2018

@yanchao12122 Could you please verify the following

  1. If you remove cv::imshow("vidoe", frame); is the memory leak still present?
int main()
{
	cv::VideoCapture cap;
	cv::Mat frame;
	while (true) {
		cap.open(0);
		cap >> frame;
		cap.release();
	}
	return 0;
}
  1. If you only open capture once, is the memory leak still present?
int main()
{
	cv::VideoCapture cap;
	cv::Mat frame;
	cap.open(0);
	while (true) 
        {
	      if(!cap.read(frame))
              {
                  break;
              }
	}
	return 0;
}
@yanchao12122

This comment has been minimized.

Copy link
Author

commented Nov 27, 2018

  1. I have test the following code in win10, vs2015, the memory still increase all the time.
int main()
{
	cv::VideoCapture cap;
	cv::Mat frame;
	while (true) {
		cap.open(0);
		cap >> frame;
		cap.release();
	}
	return 0;
}
  1. If I only open capture once, there is no memory leak existing.
@peters

This comment has been minimized.

Copy link
Contributor

commented Nov 28, 2018

@yanchao12122 Which backend are you using?

@yanchao12122

This comment has been minimized.

Copy link
Author

commented Dec 1, 2018

what's the meaning of backend?
I don't use any dnn module.

@yanchao12122

This comment has been minimized.

Copy link
Author

commented Dec 1, 2018

I don't specify any backend, just use the default.

@alalek

This comment has been minimized.

Copy link
Contributor

commented Dec 1, 2018

"Default" is auto selection which result depends on concrete environment. We need to know what backend is used.

@yanchao12122

This comment has been minimized.

Copy link
Author

commented Dec 1, 2018

I have tested the backend using "cap.open(0, cv::CAP_DSHOW)",
the memory leak just happened with this backend.

@Bleach665

This comment has been minimized.

Copy link
Contributor

commented Dec 5, 2018

I confirm memory leak. Not sure about DSHOW, but MSMF backend consume a lot of memory.
If DSHOW used mem leak is (or is it not leak) 5 Kb per 1000 iteration. 1 time (from 3) app was terminated, unfortunately I dont remember error code.
And if MSMF used - 550 Mb(1) memory leak for 1000 iteration.
Also if used MSMF
cap.release();
produce to std output warning
[ WARN:0] terminating async callback,
sometime [ WARN:x], x=1..5

OpenCV 3.4 build ccf96b9
Win 10 x64, VS 15.9.2

Code used for test:

cv::VideoCapture cap;
cv::Mat frame;
int iBackend = cv::CAP_MSMF;//or CAP_MSMF
cap.open(0, iBackend);
cap >> frame;
cap.release();
std::cout << "wait Enter";
getchar(); //check mem size here
std::cout << "start";
for (int i = 0; i < 1000;++i) {
	cap.open(0, iBackend);
	cap >> frame;
	cap.release();
}
std::cout << "end"; //end here
getchar();
@SecT0uch

This comment has been minimized.

Copy link

commented Feb 6, 2019

Exactly same problem than @Bleach665 using 4.0.0 Windows 10 x64

@filippk

This comment has been minimized.

Copy link

commented Mar 5, 2019

Same problem, opencv 4.0.1 Windows 10x64 MSVS 2015

@alalek alalek added the incomplete label Mar 5, 2019

@SecT0uch

This comment has been minimized.

Copy link

commented Mar 5, 2019

@alalek which info do you need?

@alalek alalek added the priority: low label Mar 6, 2019

@alalek

This comment has been minimized.

Copy link
Contributor

commented Mar 6, 2019

MSVS memory profiler is not able to show exact reason of the problem.
Some callbacks are not destroyed (refcount is not zero), but reason is unclear because 97% callbacks were released successfully and OpenCV's references are released.
Callback's memory consumption is about few kB, but leak is about several Mb. Also most part of allocations are not available via MSVS memory profiler.

[ WARN:0] terminating async callback,
sometime [ WARN:x], x=1..5

0,1..5 numbers mean threadID. So, the last reference of Callback has been released from some worker threads (perhaps created by MSMF).

General usage work well:

  • single VideoCapture open
  • read multiple frames
  • significant leaks are not observed

Currently, leak is observed with periodic opening/closing of VideoCapture only.
It is not primary scheme of OpenCV usage, so priority of this is low.

@yinguobing

This comment has been minimized.

Copy link

commented Mar 7, 2019

Good to know the leak is confirmed. We had switch to thread pool to avoiding frequently creating/destroying thread with VideoCapture. This could be a temporary option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.