Memory leak using copyTo UMat
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?
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: 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?
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: 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.
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
I ran into this also and posted a similar question here with some additional memory information from VS 2015. http://answers.opencv.org/question/78...
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:
but this following code not calling color convert will have the same problem you are having which is running out of memory:
Try calling some OpenCV function like resize or color convert that will queue up a OpenCL command. See if your memory problem goes away.