Memory leak in Mat::convertTo
While using warpPerspective with openCL I was having a memory leak. Disabling opencl by using Mat or calling cv::ocl::setUseOpenCL(false) solved the problem. I think I've tracked it down to a line in ocl_warpTransform that uses convertTo from Mat to UMat. Here is code to replicate this.
int main(int, char)
{
while (true)
{
Mat matM(Size(5, 5), CV_32F);
UMat M0;
matM.convertTo(M0, CV_32F);
}
}
I used VS 2015 diagnostics tool (alt-F2). Ran this program and did a memory snapshot at about 2 sec and another one 10 seconds later. Attached is a picture of the changed memory report.
Can you explain how this is going to leak? Or how you analysed and concluded the leak?
I was using the warpPerspective function and the program would run until all memory was used. If you wrap these three lines in a while(true) loop you can watch memory use go up in task manager. Changing UMat to Mat does not have the same problem. This happened using the Ceemple Visual Studio 2013 plugin binaries. I then downloaded source from github and recompiled in VS 2015. I then found the VS 2015 memory leak tools and was able to trace the problem back to this convert statement. Since then I've found that it isn't just convertTo. It also breaks with copyTo. I think the root problem is the code that allocates the UMat, but I'm reaching the limits of my technical ability. I'll keep working on it, but I felt it was serious enough to post about.
I did not notice the memory leak you mention. I monitored the memory consumption with the task manager with mat size=5000x5000. I did notice that the memory use increased by steps and then decreased and so on.
I used OpenCV 3.0.0-rc1 with VS2010 in release mode and x64 with the code:
Using your code it just bounces between 107MB and 203MB. If I change it to a 5x5 it keeps going up until all memory is gone. Can you run it again with a 5x5?
If both are UMat then it keeps going up regardless of the size.
I also duplicated this on a Mac using XCode. Both computers have Intel GPU's and drivers. Running in a VM without a GPU works as expected, useOpenCL = 0.
What is your version of OpenCV ? I changed to 5000x5000 because with 5x5 and running the program for 10 min didn't show a memory change.
I will retry soon with UMat and 5000x5000.
Have you tried to submit a bug report here ?
On Windows I downloaded the latest dev version 3 from github. On the Mac I used the gold version. I have not put in a bug report yet.
With:
The memory usage did't change at all (I run the program like for 10 min).
But if I select my Intel HD Graphics 4000, the previous code forced me to reboot my computer. So I got no problem with my NVidia graphics card.
In my opinion, I suspect a problem with the hardware but I am not competent at all. This happens with your integrated graphics chipset or with a dedicated graphics card (NVidia or AMD) ?
I think that you could safely open an issue.
I opened a bug report here. I tried 2 different computers with integrated Intel GPUs and one Mac with AMD GPU. It happened with all of them.
Thanks for submitting the issue and for spotting the bug.
Your Mac doesn't have an integrated GPU ? I asked this because when I selected my integrated GPU (when there are 2 GPUs, by default it takes the first one so the integrated I think, I added the environment variable OPENCV_OPENCL_DEVICE to select which GPU I want to use), the bug was obvious whereas it was completly fine with the NVidia.