Memory leak in Mat::convertTo

asked 2015-12-10 17:08:19 -0600

Steve Hess gravatar image

updated 2015-12-11 13:11:15 -0600

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. image description

edit retag flag offensive close merge delete

Comments

Can you explain how this is going to leak? Or how you analysed and concluded the leak?

boaz001 gravatar imageboaz001 ( 2015-12-11 03:40:36 -0600 )edit

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.

Steve Hess gravatar imageSteve Hess ( 2015-12-11 08:50:50 -0600 )edit

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:

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/ocl.hpp>
int main() {
  std::cout << "OpenCV=" << std::hex << CV_VERSION << std::endl;
  std::cout << "Build info=" << cv::getBuildInformation() << std::endl;
  cv::ocl::setUseOpenCL(true);
  std::cout << "cv::ocl::useOpenCL()=" << cv::ocl::useOpenCL() << std::endl;
  while (true) {
    cv::Mat matM(cv::Size(5000, 5000), CV_32F);
    cv::UMat M0;
    matM.convertTo(M0, CV_32F);
  }
Eduardo gravatar imageEduardo ( 2015-12-13 14:26:28 -0600 )edit

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.

Steve Hess gravatar imageSteve Hess ( 2015-12-14 11:25:52 -0600 )edit

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.

Steve Hess gravatar imageSteve Hess ( 2015-12-14 11:30:36 -0600 )edit

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 ?

Eduardo gravatar imageEduardo ( 2015-12-15 10:16:03 -0600 )edit

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.

Steve Hess gravatar imageSteve Hess ( 2015-12-15 10:20:05 -0600 )edit

With:

while (true) {
    cv::UMat matM(cv::Size(5000, 5000), CV_32F);
    cv::UMat M0;
    matM.convertTo(M0, CV_32F);
}

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.

Eduardo gravatar imageEduardo ( 2015-12-15 16:16:14 -0600 )edit
3

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.

Steve Hess gravatar imageSteve Hess ( 2015-12-15 17:00:58 -0600 )edit

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.

Eduardo gravatar imageEduardo ( 2015-12-16 03:53:52 -0600 )edit