When will resize function reallocate memory? (OpenCV 4.5.5)

I’m confused that under which condition will resize reallocate memory for dst. I’ve tried such code:

    Mat stackMat(10, 10, CV_8UC3);
    Mat b(4, 4, CV_8UC3);
    cout << (void*)stackMat.data << endl;
    resize(b, stackMat, cv::Size(7, 9), 0, 0, cv::INTER_LINEAR);
    cout << (void*)stackMat.data << endl;
    resize(b, stackMat, cv::Size(6, 7), 0, 0, cv::INTER_LINEAR);
    cout << (void*)stackMat.data << endl;

And the result is:

000001703CE0C780
000001703CE0C780
000001703CE00A00

The first and second line is identical but third is not. I think if dst’s original size is larger than expected size there will be no reallocation and it is proved by the equality of first and second line, but why third line is different?

memory allocation is magic.

memory allocators are witchcraft.

both your resize() calls should be expected to reallocate.

a reallocation may have happened but coincidentally resulted in the same memory address. this might be deterministic. the first allocation hadn’t been used yet. all subsequent allocations were actually filled with data.

imo, you would not see a new allocation, assigned to the same address this way

Thanks, but if there’s anyway to resize avoiding reallocation? e.g. I pre-allocate a large space and let all resizing operations happened in the space (assume the space is big enough to contain subseqent dst size)?

yes: resize to the same predictable size every time

then one allocation can be reused.

your question sounds like you’re resizing to various random sizes. why do you do that and why do you think allocations are an actual issue you must spend time thinking about?

have you profiled your situation? what is your situation? what are the performance numbers? can you provide a MRE?

Hi,
Thanks for your help again. Actually my sitution is not totally about resize. The reason I concern the memory allocation is that I’m meeting a memory problem and wondering if OpenCV’s allocation mechanism causes memory leak in my program.

I’m developing an android sdk using OpenCV, and my library for win10 works well (I’ve tested using VLD and it has no memory leak) but when it is deployed on JNI ( same code cross-compiled using Visual Studio), it seems cause some memory leak because after our pressure test for many hours it sometimes throw an exception:
OpenCV Error: Insufficient memory (Failed to allocate xxx bytes) in cv::OutOfMemoryError
My code is very simple and I’m sure that except OpenCV there’s no chance for memory leaking in my code.

Sorry that I cannot provide you a MRE yet because I don’t have the JNI part project, but I’ve created a small MRE in my part as follow:

#pragma once
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;
using namespace std;

class PlateRecognitionPipeline
{
public:
    PlateRecognitionPipeline();
    virtual ~PlateRecognitionPipeline() = default;

    bool Recognize(const char* srcImagePtr, const int& srcImageHeight, const int& srcImageWidth, const VehicleType& vehicleType);
};

PlateRecognitionPipeline::PlateRecognitionPipeline()
{
}

bool PlateRecognitionPipeline::Recognize(const char* srcImagePtr, const int& srcImageHeight, const int& srcImageWidth)
{
    if (srcImagePtr == nullptr)
    {
        return false;
    }
    cv::Mat yuvMat(srcImageHeight * 3 / 2, srcImageWidth, CV_8UC1, (void*)srcImagePtr);
	return true;
}

Java developer create the PlateRecognitionPipeline and call Recognize in an infinite loop. Through there’s nothing but a convertion of NV21 to BGR, it still cause the memory error above after many times of calling.

If I write a convertion function using pre-allocated memory in constructor function (and release it manually in destructor) instead of using OpenCV, this error disappears. But in my subseqent code( containing resize, copyMakeBorder, etc.) there will still be the same error.

As a result now I’m looking for some way that doesn’t allocate memory during running but allocate fixed size memory when constructing (i.e. like a memory pool).

Are there anyway to fix the oom problem? Or some reason else that may cause the error (Assuming there’s no memory leak in Java-side) ?

is this really all, there is ? function does not make much sense to me.
indeed, a Mat with a ‘foreign data pointer’ does not do any refcounting, it would be dangerous to pass this elsewhere, but as long as you do ‘nothing’ ? no.

what else do you do to ‘srcImagePtr’ ?

sorry, but where is this ?

have you tried to run some static analyzer on it ?
just saying, most attempts of my code to allocate absurd quantities were due to badly initialized vars here, lately, so –