开发者

Strange Memory Behavoir in JNI

I have found some strange behavior concerning multidimensional arrays in JNI and after hours of research I still have no idea how to solve my problem. I have the following JNI code:

JNIEXPORT jobject JNICALL Java_leaktest_NativeClass_nativeCalculation
  (JNIEnv *env, jobject thiz, jobjectArray arr) {

    double** multidimArray = new double*[512];

    for (int i = 0;i < 512;i++) {
        multidimArray[i] = new double[512];

        for (int j = 0;j < 512;j++) {
            multidimArray[i][j] = i * j;
        }
    }

    jobjectArray jMultidimArray = env->NewObjectArray(512, env->FindClass("[D"), 0);

    for (int i = 0;i < 512;i++) {
        jdoubleArray row = env->NewDoubleArray(512);

        jdouble* elems = (jdouble*)multidimArray[i];

        env->SetDoubleArrayRegion(row, 0, 512, elems);
        env->SetObjectArrayElement(jMultidimArray, i, row);

        env->DeleteLocalRef(row);
    }

    jclass arrayClass = env->FindClass("leaktest/ArrayClass");

    jobject arrObj = env->NewObject(arrayClass, env->GetMethodID(arrayClass, "<init>开发者_StackOverflow社区", "(II[[D)V"), 512, 512, jMultidimArray);

    for (int i = 0;i < 512;i++) {
        delete multidimArray[i];
    }

    delete multidimArray;

    return arrObj;
}

In Java, I simply call this native Method repeatedly. With every call the displayed amount of RAM occupied by the JVM increases by about 1 to 2 MB. It appears to me that I am somewhere allocating memory in the C++ part and never releasing it, but I have no idea where that should occur.

Michael


My guess is it would be here

jclass arrayClass = env->FindClass("leaktest/ArrayClass");

jobject arrObj = env->NewObject(arrayClass, env->GetMethodID(arrayClass, "<init>", "(II[[D)V"), 512, 512, jMultidimArray);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜