引入OpenCV导致私有内存巨大
opencvC++VS2015说明
在调试程序的时候 发现自己的程序在VS的调试窗口占用很高,
花时间关注了一下这个问题, 手动写了小的程序复现这个问题,最终确定了占用巨大的问题.
VS的调试工具表示的内存占用是统计的程序 私有字节 的大小, 而任务管理器统计的内存占用是程序的 工作集 大小,具体二者的内存的区别可以参考问题 Virtual Size causing program to run out of memory 的答案,
- Private bytes tells you how many bytes of virtual device memory your process has mapped, excluding virtual memory shared with other processes (e.g. mapped files, global heap, etc).
- Working set tells you how many bytes of physical memory you are actively using. This includes physical memory, device buffers, and mapped files. It's a pretty strange figure, since it equates to touched physical memory + mapped virtual non-system memory. In general, you should completely ignore this figure. It's practically useless for debugging memory leaks.
- Virtual bytes is the total amount of virtual memory you have mapped.
解决
通过不断的重复和二分, 最终确定了是 OpenCV引入的头文件问题, 在我们平时写OpenCV的程序的时候, 总是简单的使用 #include <opencv.hpp>
引入一个头文件来解决问题, 也是官方推荐的写法, 十分方便,
但是巨大, 我们分别使用三种方式测试, 分别是
-
不引入 Opencv 头文件, WorkingSet: 31,636 KB, PrivateByte: 6,224 KB ; VS: 7 MB
-
引入 一个 Opencv.hpp 头文件, WorkingSet: 49,012 KB, PrivateByte: 999,068 KB VS:1 GB
-
引入 一个 Opencv.hpp 头文件, WorkingSet: 31,700 KB, PrivateByte: 6,252 KB VS:7 MB
查看 opencv.hpp
文件里面的内容 最终通过二分排查, 确定了 只要引用 opencv2/dnn.hpp
之后,占用巨大, 本项目中不会用到这个头文件所以,这里可以不加入即可,问题解决!!
// #include "opencv2/opencv_modules.hpp"
//#include "opencv2/calib3d.hpp"
//#include "opencv2/features2d.hpp"
#include "opencv2/dnn.hpp" // 最终确定占用巨大
//#include "opencv2/flann.hpp"
//#include "opencv2/highgui.hpp"
//#include "opencv2/imgcodecs.hpp"
//#include "opencv2/imgproc.hpp"
//#include "opencv2/ml.hpp"
//#include "opencv2/objdetect.hpp"
//#include "opencv2/photo.hpp"
//#include "opencv2/shape.hpp"
//#include "opencv2/stitching.hpp"
//#include "opencv2/superres.hpp"
//#include "opencv2/video.hpp"
//#include "opencv2/videoio.hpp"
//#include "opencv2/videostab.hpp"
更多
图中程序部分是 VS2015 , 右侧进程管理查看软件是 ProcessExplorer_v16.21 可以查看进程的各种细节
参考链接
无