-
-
Notifications
You must be signed in to change notification settings - Fork 56.3k
Description
System Information
OpenCV version: 4.12.0 (built from source) (Also tried 4.5.4)
OS: Ubuntu 22.04.5
Compiler: g++ 11.4.0
Hardware: i5-12600K, 10cores
Detailed description
Issue description
When running ArUco marker detection (cv::aruco::ArucoDetector) in multithreaded contexts, memory usage increases significantly and is not released after threads complete.
The leak does not occur in single-threaded execution. In multi-threaded runs, RSS grows with each iteration and remains high even after images go out of scope.
Observed behavior
Memory usage increases steadily with each multithreaded detection iteration.
Peak memory ~8 GB after only 10 iterations.
Memory does not return to baseline after threads exit.
Example output:
Initial memory [GB]: 0.07
After 1 iteration [GB]: 6.7
After 10 iterations [GB]: 8.1
Final memory [GB]: 7.7
Expected behavior
Memory should remain stable (as it does in single-threaded runs).
Any temporary allocations inside ArucoDetector::detectMarkers should be released once threads complete.
Steps to reproduce
main.cpp
#include <thread>
#include <opencv2/aruco.hpp>
#include <opencv2/opencv.hpp>
#include <fstream>
#include <future>
#include <iostream>
#include <string>
#include <thread>
#include <unistd.h> // For sysconf
double get_memory_usage() {
std::this_thread::sleep_for(std::chrono::seconds(5));
std::ifstream stat_stream("/proc/self/stat", std::ios_base::in);
// Parse /proc/self/stat for vsize and rss
std::string pid, comm, state, ppid, pgrp, session, tty_nr;
std::string tpgid, flags, minflt, cminflt, majflt, cmajflt;
std::string utime, stime, cutime, cstime, priority, nice;
std::string O, itrealvalue, starttime;
unsigned long vsize;
long rss;
stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr >> tpgid >> flags >> minflt >> cminflt >>
majflt >> cmajflt >> utime >> stime >> cutime >> cstime >> priority >> nice >> O >> itrealvalue >> starttime
>> vsize >> rss;
stat_stream.close();
<!-- Failed to upload "main.cpp" -->
long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024;
auto vm_usage = vsize / 1024.0; // Convert to KB
auto resident_set = rss * page_size_kb; // Convert to KB
return (vm_usage + resident_set) / 1024 / 1024;
}
int main() {
{
auto image = cv::imread("../apriltag_16h5_10-11-12-13.jpg");
auto detect = [&](int dictId) {
auto dictionary = cv::aruco::getPredefinedDictionary(dictId);
cv::aruco::DetectorParameters detector_params;
detector_params.cornerRefinementMethod = cv::aruco::CORNER_REFINE_APRILTAG;
cv::aruco::ArucoDetector detector(dictionary, detector_params);
std::vector<int> ids;
std::vector<std::vector<cv::Point2f>> corners, rejected;
detector.detectMarkers(image, corners, ids, rejected);
};
std::cout << "Initial memory [GB]: " << get_memory_usage() << std::endl;
for (int i = 0; i < 10; ++i) {
std::vector<std::thread> threads;
for (int j = 0; j < 10; ++j) {
threads.emplace_back(detect, cv::aruco::DICT_APRILTAG_16h5);
}
for (auto &t : threads) t.join();
std::cout << "After " << i+1 << " iterations [GB]: " << get_memory_usage() << std::endl;
}
}
std::cout << "Final memory [GB]: " << get_memory_usage() << std::endl;
}
Issue submission checklist
- I report the issue, it's not a question
- I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- I updated to the latest OpenCV version and the issue is still there
- There is reproducer code and related data files (videos, images, onnx, etc)