Skip to content

memory leak / fragmentation issue #15526

@dgel

Description

@dgel
System information (version)
  • OpenCV => 4.1.1
  • Operating System / Platform => Ubuntu 18.04
  • Compiler => GCC 7.4.0
Detailed description

When creating large Mat objects followed by smaller allocations that are kept in a loop, resident memory keeps growing. This does not occur when the matrices are smaller or an allocation does not happen in between. If left to keep growing, the system will run out of memory and lock up, even though no more than 2000 small strings and 2 matrices should be in memory.

I've tried with OpenCV 3.4 as well, and the same thing happens

Steps to reproduce

compile the following, run it and keep an eye on memory.

#include <thread>
#include <utility>
#include <vector>
#include <opencv2/core/mat.hpp>

int main() {
  
    // minimum size for leak on my machine: 558995 x 1
    cv::Size size(600000, 5);
    cv::Mat tmp(size, CV_8UC3, cv::Scalar(0, 0, 0));

    std::vector<std::string> strings;

    for (size_t i = 0; i < 2000; ++i)
    {
      cv::Mat tmp2(size, CV_8UC3, cv::Scalar(0, 0, 0));
      tmp = tmp2;

      // needs to be suitably large, so it gets allocated on the heap
      std::string val("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
      strings.push_back(val);

      // gives you time to kill the process before it runs out of memory
      std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}

update:

this appears to be an issue with the usage of posix_memalign: if I remove the checks for it in the CMakeLists.txt and remove the checks for it then recompile, the leak disappears. Strangely, just allocating with posix_memalign instead of using cv::Mat does not create the issue on its own, there seems to be some interaction with the other operations cv::Mat does when creating it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions