Ask Your Question

memory leak with VideoCapture

asked 2016-08-26 07:11:30 -0500

NePher gravatar image

Hello, I was debuging my code when I saw a memory leak with VideoCapture. There you have the memory of my computer : it's go up realy fast

I tested this two code :

while (1) { VideoCapture test; test = VideoCapture(0); test.~VideoCapture(); }

while (1) { VideoCapture test; test = VideoCapture(0); test.release(); }

is this normal? do you have any solution?

Thank you

edit retag flag offensive close merge delete


Its because you are trying to destroy the objects itself. This is not needed in OpenCV because going out of scope will trigger the garbage collector destroying the item itself and freeing the memory. Secondly, why do you create objects inside while loops?

StevenPuttemans gravatar imageStevenPuttemans ( 2016-08-26 07:30:36 -0500 )edit

yes, this is normal. (at least on some os / capture driver combination)

you're supposed to use exactly 1 instance of VideoCapture, and leave it alive for the duration of your program.

berak gravatar imageberak ( 2016-08-26 07:32:38 -0500 )edit

Thank you, here it's a simplification of my code, I found a solution by puting test in a static class, that permit to never recall it

NePher gravatar imageNePher ( 2016-08-26 08:17:01 -0500 )edit

btw, os / opencv version ?

berak gravatar imageberak ( 2016-08-26 10:08:09 -0500 )edit

@berak where I can find this info ? ...supposed to use exactly 1 instance of VideoCapture... should the doc be updated ?

pklab gravatar imagepklab ( 2016-08-26 10:52:35 -0500 )edit

Ooencv 310, visual studio 2015, vc14 ans windows 10

NePher gravatar imageNePher ( 2016-08-26 11:17:47 -0500 )edit

I did a test : i did 100 instance of vidéo capture, then did a pause and magic : the memory free itself. But i had to wait some seconds

NePher gravatar imageNePher ( 2016-08-26 11:24:15 -0500 )edit

And @pklab you can do instance as much as you want, the problème was the memory wasen't free just after they were release

NePher gravatar imageNePher ( 2016-08-26 11:26:12 -0500 )edit

@pklab, shame on me, but i made this up. and i guess it's much more of a problem with v4l on linux, than on win( dshow seems just slow, destroying the filtergraph)

berak gravatar imageberak ( 2016-08-26 23:54:42 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2016-08-26 12:48:41 -0500

pklab gravatar image

updated 2016-08-26 12:55:23 -0500

Sincerely I ran this code

int i=0;
while (i<200) {
  VideoCapture test; test = VideoCapture(0); test.release();

to get this results

Duration: ~5min
Physical Memory Available: min ~4,597,000K max ~5,016,000K

image description

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-08-26 07:11:30 -0500

Seen: 1,344 times

Last updated: Aug 26 '16