码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2017-07-03 11:58:33      阅读:380      评论:0      收藏:0      [点我收藏+]

标签:sel   har   table   new   splay   fopen   sizeof   ref   odi   

原因是没即时释放对象,原本的代码是这样

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)

标签:sel   har   table   new   splay   fopen   sizeof   ref   odi   

原文地址:http://www.cnblogs.com/formatfa/p/7109674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!