Garbage collection and JNI call

后端 未结 3 1009
庸人自扰
庸人自扰 2021-01-07 00:07

I am having an issue with a JNI program randomly running out of memory.

This is a 32 bit java program which reads a file, does some image processing, typically using

3条回答
  •  -上瘾入骨i
    2021-01-07 00:31

    My own approach to this problem is simply to call System.gc(), but from inside the native code:

    #include 
    // ...
    int my_native_function(JNIEnv* env, jobject obj) {
        jclass    systemClass    = nullptr;
        jmethodID systemGCMethod = nullptr;
        // ...
        // Take out the trash.
        systemClass    = env->FindClass("java/lang/System");
        systemGCMethod = env->GetStaticMethodID(systemClass, "gc", "()V");
        env->CallStaticVoidMethod(systemClass, systemGCMethod);
    }
    

    I hope this works for you, too.

提交回复
热议问题