首页 > 代码库 > 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)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。