I created a dynamic library that links to OpenCV’s static library. Within the external interface, I created cv::Mat objects. In the test code, a for loop continuously loaded and unloaded the dynamic library, causing the memory committed by the current process to increase slowly.
OpenCV version:4.6.0
Compiler version:vs2019
Dynamic library code:
#include <opencv2/opencv.hpp> #include <opencv2/aruco.hpp> #include <windows.h> int fun_opencv(int width, int height, std::string filepath) { cv::Mat img(height, width, CV_16UC1); int ret = 0; for (int i = 0; i < width * height; ++i) { ret += 1; } Sleep(10); return ret; } int fun_opencv_contrib(int width, int height, std::string filepath) { int ret = 0; for (int i = 0; i < width * height; ++i) { ret += 1; } return ret; }
test code:
#include <windows.h>
#include
#include <psapi.h>
#includetypedef int(__cdecl* Fun_opencv)(int, int, std::string);
typedef int(__cdecl* Fun_opencv_contrib)(int, int, std::string);
typedef int(__cdecl* Fun_cleanup_opencv)();
typedef int(__cdecl* Fun_InitOpenCVPool)();
typedef int(__cdecl* Fun_CleanupOpenCVPool)();
//
HMODULE hMod = NULL;int fun_test();
int main() {
int ret;
//
//hMod = LoadLibrary(L"libOpenCV.dll");
std::ofstream log(“memory_trace.txt”); // ① open log
if (!log) { std::cerr << “create log failed\n”; return 1; }
for (size_t i = 0; i < 1000; i++)
{
ret= fun_test();
if (ret == 0) {
PROCESS_MEMORY_COUNTERS pmc{};
if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) {
log << i << " —> memory occupied by the current process = " << (pmc.WorkingSetSize / 1024 / 1024) << " MB"<< std::endl;
log.flush();
std::cout << i << " —> memory occupied by the current process = " << (pmc.WorkingSetSize / 1024 / 1024) << " MB" << std::endl;
}
}
}
//FreeLibrary(hMod);
return 0;
}int fun_test() {
//
HMODULE hMod = LoadLibrary(L"libOpenCV.dll");
if (!hMod) {
std::cerr << "LoadLibrary failed, error = " << GetLastError() << “\n”;
return 1;
}
FreeLibrary(hMod); //
return 0;}
Run the test code and observe the printed information to see that the memory committed by the current process is increasing.