OpenCV保存内存的时间比需要的时间长
我有一个程序,可以执行以下操作:OpenCV保存内存的时间比需要的时间长,opencv,Opencv,我有一个程序,可以执行以下操作: #include <iostream> #include "opencv2/core.hpp" int main() { for (int i = 0; i < 10; ++i) { cv::Mat mats[10]; for (auto & m : mats) { m = cv::Mat::zeros(5000, 5000, CV_8U);
#include <iostream>
#include "opencv2/core.hpp"
int main() {
for (int i = 0; i < 10; ++i) {
cv::Mat mats[10];
for (auto & m : mats) {
m = cv::Mat::zeros(5000, 5000, CV_8U);
// use the mats ...
}
}
getchar(); // at this point, process holds hundreds of megabytes of memory
}
此代码之后,该进程占用的内存几乎为零。
我猜OpenCV有一些内部内存管理,它不会释放内存以供将来使用。
这对我来说是个问题,因为在这个代码运行之后,我需要这个内存来做其他事情。此进程用作服务器,因此它将无限期地保持运行。有没有办法强制OpenCV释放此内存
我在Ubuntu 16.04上使用OpenCV 4.3.0请发布一个最简单的工作示例如果你在不需要垫子后手动
释放它们,内存占用是多少?也许这会有帮助。使用opencv Mat类后,请使用release。@eldesgraciado没有effect@Miki添加了一个最小的工作示例
#include <iostream>
#include "opencv2/core.hpp"
int main() {
for (int i = 0; i < 10; ++i) {
cv::Mat mats[10];
for (auto & m : mats) {
m.data = new uchar[5000 * 5000];
// use the memory ...
memset(m.data, i, 5000 * 5000);
}
for (auto & m : mats) {
delete m.data;
}
}
getchar(); // at this point, process holds almost zero memory
}