[AndroidTip]local reference table overflow (max=512)的錯誤解決

JNI層coding經常會遇到ReferenceTable overflow問題,特別是當jni函數被反覆調用上千上萬次的時候,現彙總如下,未完待續,並歡迎補充,(*^__^*) 嘻嘻……

總體原則:釋放所有對object的引用

1.FindClass

例如,

jclass ref= (env)->FindClass("java/lang/String");

env->DeleteLocalRef(ref);

 

2.NewString/ NewStringUTF/NewObject/NewByteArray

例如,

jstring     (*NewString)(JNIEnv*, const jchar*, jsize);   

const jchar* (*GetStringChars)(JNIEnv*, jstring, jboolean*);    

void        (*ReleaseStringChars)(JNIEnv*, jstring, const jchar*);

jstring     (*NewStringUTF)(JNIEnv*, const char*);   

const char* (*GetStringUTFChars)(JNIEnv*, jstring, jboolean*);    

void        (*ReleaseStringUTFChars)(JNIEnv*, jstring, const char*);

env->DeleteLocalRef(ref);

 

3.GetObjectField/GetObjectClass/GetObjectArrayElement

jclass ref = env->GetObjectClass(robj);

env->DeleteLocalRef(ref);

 

4.GetByteArrayElements

jbyte* array= (*env)->GetByteArrayElements(env,jarray,&isCopy);

(*env)->ReleaseByteArrayElements(env,jarray,array,0);

 

4.const char* input =(*env)->GetStringUTFChars(env,jinput, &isCopy);

(*env)->ReleaseStringUTFChars(env,jinput,input);

 

5.NewGlobalRef/DeleteGlobalRef

 jobject     (*NewGlobalRef)(JNIEnv*, jobject);    

void        (*DeleteGlobalRef)(JNIEnv*, jobject);

例如,

jobject ref= env->NewGlobalRef(customObj);

env->DeleteGlobalRef(customObj);

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章