android 释放 NDK 内存

问题描述 投票:0回答:0

我有一个 android 项目,我在其中使用 NDK 进行一些图像处理/识别任务,我使用的是基于 opencv 的本机库。 我有一个帮助程序类来调用本机函数,我在单例对象中加载本机库。 问题是内存消耗非常高,我用Android studio中的profiler监控内存,一开始内存消耗在70MB左右,一旦调用native库,它就跳到400MB。真正的问题是我在完成所需的过程后无法释放此内存使用量,应用程序不再需要使用本机库,我尝试释放 Java 单例对象,但这并没有释放内存。有没有办法释放原生对象,或者卸载原生库?

这是我的助手类代码

public class JniHelper {
private static final String TAG = "JniHelper";

static
{
    System.loadLibrary("native-lib");
}

private static com.jin.facesample.JniHelper instance;
private static MyFaceEngine faceEngine = new MyFaceEngine();

public static com.jin.facesample.JniHelper getInstance() {
    if (instance == null) {
        instance = new com.jin.facesample.JniHelper();
    }
    return instance;
}

public static void releaseInstance(){
    
    faceEngine= null;
    instance=null;
    System.gc();
    System.gc();
}

private JniHelper(){

}

// my native functions calls ..... 

}

android c++ opencv android-ndk
© www.soinside.com 2019 - 2024. All rights reserved.