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);
source share