Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to free cv::Mat from memory

I have a sequence of images (frames) from a camera as an input, what is stored in std::queue<cv::Mat> buffer container. After the processing algorithm is finished for a frame then it is popped. My problem is that somehow the memory usage is constantly rising, however the container size buffer.size() is maximum 1 (so the algorithm is 'eating' up the buffer properly and pops the images).

I would like demonstrate my problem with a minimal reproducible code:

cv::Mat current_frame(500, 500, CV_16UC3);
std::queue<cv::Mat> buffer;
while(true)
{
    buffer.push(current_frame.clone());
    buffer.front().release();
    buffer.pop();
}

This code results in constantly rising memory usage. (Ubuntu system, opencv 3.4.6 version)

My question is that how can you properly release a cv::Mat from memory.