JNI内存泄露JNI ERROR (app bug): local reference table overflow (max=512)

莫问天涯路几重,轻衫侧帽且从容。这篇文章主要讲述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)】 

    推荐阅读