opencv - Java OpenCV错误占用读取图像的内存空间
问题描述
即使我说frame = null 对读取的蒙版图像没有任何操作,垃圾收集器也无法清除它。如果我不执行frame.get(),则会出现内存溢出。当我使用 frame.get () 执行此操作时,垃圾收集器正在清理。
除了我正在考虑的这种方法之外,还有其他人有什么建议吗?
public class Temp3 {
private ScheduledExecutorService timer;
private VideoCapture capture;
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Temp3 temp2 = new Temp3();
temp2.name2();//memory overflow occurs.
}
private void name2() {
capture = new VideoCapture(0);
if (capture.isOpened()) {
Runnable frameGrabber = new Runnable() {
@Override
public void run() {
if (capture.isOpened()) {
Mat frame = new Mat();
capture.read(frame);
int width = frame.width(), height = frame.height(), channels = frame.channels();
byte[] sourcePixels = new byte[width * height * channels];
frame.get(0, 0, sourcePixels);
frame = null;
}
}
};
this.timer = Executors.newSingleThreadScheduledExecutor();
this.timer.scheduleAtFixedRate(frameGrabber, 0, 33, TimeUnit.MILLISECONDS);
}
}
}
解决方案
推荐阅读
- java - Eclipse Collections 通过自定义的类似比较器的函数对 IntList 进行排序,无需装箱
- java - 如何从java中的HashMap中提取值?
- java - Spring-Data-JPA ManyToMany 与额外列的关系
- aerospike - Aerospike:有没有办法将流 UDF 发布到 UI?
- c# - 从数据表列中提取
- css - 如何从靠近右边缘的最后一个元素中删除边距?
- opengl - 为什么加零会改变顶点着色器的结果
- shell - SHELL 自动从 crontab 中杀死进程
- reactjs - 如何使用 React 和 Redux Form 从不同的组件调用函数?
- dart - 颤振缺少打印实现