首页 > 解决方案 > 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);
    }
}

}

标签: opencv

解决方案


推荐阅读