【问题标题】:OpenCV Error in Android: Insufficient memory (Failed to allocate)Android中的OpenCV错误:内存不足(分配失败)
【发布时间】:2018-07-15 11:31:50
【问题描述】:

我在 Android Studio 中使用 opencv 来增加图像中的红色比例。但是当我运行该函数大约 30 次后,程序崩溃并显示错误。

OpenCV Error: Insufficient memory(Failed to allocate *****bytes)

我搜索了很多相关问题,很多人说添加 Mat.release() 可以解决这个问题。我在我的函数中添加了 Mat.release 但它没有帮助。程序运行超过 30 次后仍然崩溃。

这是我的代码。有人知道如何解决这个问题吗?

public void addRedColor(int red){

Mat img = new Mat();
Utils.bitmapToMat(src, img);

List<Mat> bitplane = new ArrayList<>(img.channels());
Core.split(img, bitplanes);
Mat redChannel = new Mat();
Core.add(bitplane.get(0), new Scalar(red), redChannel);
bitplane.set(0, redChannel);
Core.merge(bitplane, img);

// release the Mat
img.release();
bitplane.get(0).release();
bitplane.get(1).release();
bitplane.get(2).release();
redChannel.release();

}

【问题讨论】:

  • 每个new 调用都应该有对应的delete 调用。注意release 是另外一回事,它只会释放 Mat 的一些内部字段
  • @DmitriiZ。它是 Java,而不是 C++
  • 我也是java和android的新手,我尝试在java中删除 Mat。但是java中OpenCV好像没有delete功能。

标签: android opencv memory-management out-of-memory


【解决方案1】:

避免新Mat浪费内存,在方法结束前强制垃圾收集器

    System.gc();
    System.runFinalization();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2018-11-10
    • 2016-12-19
    • 2014-11-23
    • 2019-06-28
    • 1970-01-01
    相关资源
    最近更新 更多