简体   繁体   中英

JNI, I can not find the code place of memory leak

Here is the jni cpp side code:

JNIEXPORT jfloatArray JNICALL Java_server_framework_chat_similarity_TextMatcher_xgboostBatchScore
      (JNIEnv *env, jobject obj, jlong long_h, jobject q1, jobject q2){

        jclass ret = env->FindClass("java/util/ArrayList");
        jclass java_util_ArrayList = static_cast<jclass>(env->NewGlobalRef(ret));
        jmethodID java_util_ArrayList_ = env->GetMethodID(java_util_ArrayList, "<init>", "(I)V");
        jmethodID java_util_ArrayList_size = env->GetMethodID (java_util_ArrayList, "size", "()I");
        jmethodID java_util_ArrayList_get = env->GetMethodID(java_util_ArrayList, "get", "(I)Ljava/lang/Object;");
        jmethodID java_util_ArrayList_add = env->GetMethodID(java_util_ArrayList, "add", "(Ljava/lang/Object;)Z");

        jint len = env->CallIntMethod(q1, java_util_ArrayList_size);
        vector<string> result1;
        result1.reserve(len);
        for (jint i=0; i<len; i++) {
          jstring element = static_cast<jstring>(env->CallObjectMethod(q1, java_util_ArrayList_get, i));
          const char* pchars = env->GetStringUTFChars(element, NULL);
          result1.push_back(pchars);
          env->ReleaseStringUTFChars(element, pchars);
          env->DeleteLocalRef(element);
        }  

        jint len2 = env->CallIntMethod(q2, java_util_ArrayList_size);
        vector<string> result2;
        result2.reserve(len2);
        for (jint i=0; i<len2; i++) {
          jstring element = static_cast<jstring>(env->CallObjectMethod(q2, java_util_ArrayList_get, i));
          const char* pchars = env->GetStringUTFChars(element, NULL);
          result2.push_back(pchars);
          env->ReleaseStringUTFChars(element, pchars);
          env->DeleteLocalRef(element);
        }  

        HANDLE_XGBOOST_SESSION h = (HANDLE_XGBOOST_SESSION)long_h;
        vector<float> scores = get_xgboost_batch_score(h, result1, result2);

        jfloatArray result = env->NewFloatArray(len);
        jfloat fill[len];
        for (int i = 0; i < scores.size(); i++) {
             fill[i] = scores[i]; 
        }
        env->SetFloatArrayRegion(result, 0, len, fill);

        env->DeleteLocalRef(ret);
        env->DeleteGlobalRef(java_util_ArrayList);
        env->DeleteLocalRef(q1);
        env->DeleteLocalRef(q2);
        return result;
      }

The meaning of the get_xgboost_batch_score is: get the matching score of two text in batch.

I start 5 simple java test programs which contains a while loop to run this code.

The memory monitor shows that the memory decrease about 50M in 30min.

But I try my best and can not find the bug code.

I use valgrind to track the java program and find:

==13595== Invalid write of size 4
==13595==    at 0x80CC692: ???
==13595==    by 0x80B84E6: ???
==13595==    by 0x64CB649: ??? (in /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/jre/lib/amd64/server/libjvm.so)
==13595==    by 0x64DE14E: ??? (in /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/jre/lib/amd64/server/libjvm.so)
==13595==    by 0x64E92B2: ??? (in /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/jre/lib/amd64/server/libjvm.so)
==13595==    by 0x767A3C5A: JNIEnv_::CallIntMethod(_jobject*, _jmethodID*, ...) (jni.h:987)

Is CallIntMethod the bug?

There are no memory leak.

The memory do not decrease after about 5h.

没有内存泄漏

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM