# Frequent loading/unloading of dynamic libraries increases memory usage

**URL:** <https://forum.opencv.org/t/frequent-loading-unloading-of-dynamic-libraries-increases-memory-usage/24132>\
**Category:** C++\
**Created:** [October 16, 2025, 6:02am UTC](https://forum.opencv.org/t/frequent-loading-unloading-of-dynamic-libraries-increases-memory-usage/24132 "2025-10-16T06:02:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![nanshu](https://avatars.discourse-cdn.com/v4/letter/n/a87d85/32.png) [@nanshu](https://forum.opencv.org/u/nanshu)\
**Post date:** [October 16, 2025, 6:02am UTC](https://forum.opencv.org/t/frequent-loading-unloading-of-dynamic-libraries-increases-memory-usage/24132/1 "2025-10-16T06:02:03Z")

</div>

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:
> 
> ```auto
> #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\>  
> #include
> 
> typedef 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.
