OpenCV Matrix memory release after imread command

I have a for loop in which I create a local cv :: Mat object to store the image. The code is as follows:

for (int iter = 0; iter < totalNumberOfIterations; iter++) { cv::Mat I = cv::imread(argv[1], 0); std::cout << "Reference count I: " << *I.refcount << std::endl; I.release(); } 

During the first iteration of the loop, I found that memory was allocated for the variable "I", and it is freed up when I.release () is called. During subsequent iterations, the memory is not freed, the RAM consumption for my program remains constant. OpenCV seems to reserve memory for variable "I" for optimization purposes. It's true?

The reference counter for the variable "I" (* I.refcount) remains at 1 through all iterations of the for loop.

I am using OpenCV 2.4.4 and I am compiling my code with gcc 4.6.4. To check memory consumption, I used the "top" command in my Ubuntu 13.04 machine.

EDIT: When I do not force OpenCV to read the grayscale image, I notice that memory is being freed for variable "I". (Note that the second parameter is set to "1" in the imread command.)

 cv::Mat I = cv::imread(argv[1], 1); 
+6
source share
1 answer

Did you try to declare Mat before the for loop, rewriting it for each iteration, and then freeing it?

those.

 cv::Mat I; for (int iter = 0; iter < totalNumberOfIterations; iter++) { I = cv::imread(argv[1], 0); std::cout << "Reference count I: " << *I.refcount << std::endl; } I.release(); 

Of course, this does not solve the underlying problem that it issues only once, but it seems to me that it will have the same effect. Or is there a reason you don't want to do this?

+1
source

Source: https://habr.com/ru/post/952955/


All Articles