JNI内存泄露JNI ERROR (app bug): local reference table overflow (max=512)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JNI内存泄露JNI ERROR (app bug): local reference table overflow (max=512)相关的知识,希望对你有一定的参考价值。
原因是没即时释放对象,原本的代码是这样
static jobject getMaps(JNIEnv *env,jclass obj) { jclass stringbuilder_class = (*env)->FindClass(env,"java/lang/StringBuilder"); jmethodID init_stringbuilder_Method = (*env)->GetMethodID(env,stringbuilder_class,"<init>","()V"); jobject stringbuilder_obj=(*env)->NewObject(env,stringbuilder_class,init_stringbuilder_Method); jmethodID append_Method = (*env)->GetMethodID(env,stringbuilder_class,"append","(Ljava/lang/Object;)Ljava/lang/StringBuilder;"); FILE * fileP; char item[200]="ii"; fileP = fopen( "/proc/self/maps","r"); if(!fileP) return stringbuilder_obj; while(fgets(item,sizeof(item),fileP)!=NULL) { jstring str = (*env)->NewStringUTF(env,item); (*env)->CallObjectMethod(env,stringbuilder_obj,append_Method,str); (*env)->ReleaseStringUTFChars(env,str,item); (*env)->DeleteLocalRef(env,str); } return stringbuilder_obj; }
直接看去好像没什么问题,但是每调用一次 (*env)->CallObjectMethod(env,stringbuilder_obj,append_Method,str)
;都会产生个对象,我一开始以为这样没事,改成这样程序就不崩了
static jobject getMaps(JNIEnv *env,jclass obj) { jclass stringbuilder_class = (*env)->FindClass(env,"java/lang/StringBuilder"); jmethodID init_stringbuilder_Method = (*env)->GetMethodID(env,stringbuilder_class,"<init>","()V"); jobject stringbuilder_obj=(*env)->NewObject(env,stringbuilder_class,init_stringbuilder_Method); jmethodID append_Method = (*env)->GetMethodID(env,stringbuilder_class,"append","(Ljava/lang/Object;)Ljava/lang/StringBuilder;"); FILE * fileP; char item[200]="ii"; fileP = fopen( "/proc/self/maps","r"); if(!fileP) return stringbuilder_obj; while(fgets(item,sizeof(item),fileP)!=NULL) { jstring str = (*env)->NewStringUTF(env,item); //加个temp用来释放 jobject temp=(*env)->CallObjectMethod(env,stringbuilder_obj,append_Method,str); (*env)->ReleaseStringUTFChars(env,str,item); (*env)->DeleteLocalRef(env,str); (*env)->DeleteLocalRef(env,temp); } return stringbuilder_obj; }
以上是关于JNI内存泄露JNI ERROR (app bug): local reference table overflow (max=512)的主要内容,如果未能解决你的问题,请参考以下文章
JNI ERROR (app bug): accessed stale local reference
Android : JNI ERROR (app bug): local reference table overflow (max=512)
JNI ERROR (app bug): local reference table overflow (max=512)
JNI ERROR (app bug): local reference table overflow (max=512)
Android Webrtc JNI ERROR (app bug): local reference table overflow (max=512)
JNI ERROR (app bug): local reference table overflow (max=512) Android