莫问天涯路几重,轻衫侧帽且从容。这篇文章主要讲述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)】
推荐阅读
- 如何找到Android app启动activity和页面元素信息
- Android图片左右移动
- AndroidManifest
- 13.Android插件化入门指南
- Visual Studio Emulator for Android 折腾记
- 关于九大内置对象的补充——application
- 10多种PC最佳文件共享软件(支持在线和离线传输文件)
- 不能错过的7款最佳编剧软件合集 [免费和付费]
- 10大最佳电子表格软件列表(满足你个人和业务需求)