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) ?