Memory leak using copyTo UMat

asked 2015-10-30 07:37:09 -0600

Kåre Jensen gravatar image

I am having issues converting a Mat image to a UMat using the copyTo method without introducing a memory leak using the OpenCV 3.0.

I have tried tested the using the two code snippets below trying to trace process memory usage step by step: In both cases the orgFrame is a Mat image already allocated and assiged.

Mat version:

Mat frameMat;
orgFrame.copyTo(frameMat); // Memory allocated
frameMat.release(); // Memory deallocated

UMat version:

UMat frameUMat;
orgFrame.copyTo(frameUMat); // Memory allocated
frameUMat.release(); // Expected Memory deallocated - But Not???

I have tracked the memory usage by usin the Performance Monitor (perfmon) Windows tool monitoring the Process - Working Set – Private value.

Can anybody give me an explanation of why the frameUMat.release is not releasing memory as I expect or is this some kind of memory leak bug in the OpenCV code?

edit retag flag offensive close merge delete


I think that when UMat is used and opencl or CUDA are activated in your opencv configuration UMat memory allocated is not in computer RAM but in GPU ram. So I don't think windows tool can watch this memory. You have to use for example Nsight for this purpose

LBerger gravatar imageLBerger ( 2015-10-30 08:02:10 -0600 )edit

@LBerger: You might be right about not measuring the memory correctly. But then on the other hand I would not then expect to get a increase in memory usage after the copyTo method is called?

Kåre Jensen gravatar imageKåre Jensen ( 2015-10-30 08:14:27 -0600 )edit

I think that's first call to opencl or cuda. You can check this : insert UMat m(1,1,CV_8UC1); at beginning of your program. In my program (VS2013 opencv 3.0) this UMat need 6.3Mo (in debug mode). Many dll are loaded

LBerger gravatar imageLBerger ( 2015-10-30 08:25:58 -0600 )edit

@LBerger: That sounds plausible. It is actual a leak of ~6MB I see. But then how to deallocate these 6MB again? Calling frameUMat.release does not? And as I like to convert a Mat to UMat in a Video Reader loop I see this ~6MB leak in loop, and that is fast getting quite heavy.

Kåre Jensen gravatar imageKåre Jensen ( 2015-10-30 08:52:56 -0600 )edit

If you want to free this 6 Mb I think that you have to unload dll intelopencl. I don't think there is a memory leak here. you can check this sample memory used is constant

LBerger gravatar imageLBerger ( 2015-10-30 09:23:53 -0600 )edit

I ran into this also and posted a similar question here with some additional memory information from VS 2015.

Steve Hess gravatar imageSteve Hess ( 2015-12-11 19:33:31 -0600 )edit

I found a similar problem and a simple workaround I don't know why works. Simply use the UMat with some OpenCV call. For example this code works:

   cv::UMat inMat, outMat;
   vidInput >> inMat;
   cv::cvtColor(inMat, outMat, cv::COLOR_RGB2GRAY);
   vidOutput << outMat;

but this following code not calling color convert will have the same problem you are having which is running out of memory:

   cv::UMat inMat, outMat;
   vidInput >> inMat;
//   cv::cvtColor(inMat, outMat, cv::COLOR_RGB2GRAY);
   vidOutput << outMat;

Try calling some OpenCV function like resize or color convert that will queue up a OpenCL command. See if your memory problem goes away.

browniegirl gravatar imagebrowniegirl ( 2016-01-23 19:46:37 -0600 )edit