0
我是一個JNI的新手,在我測試的手機之一,我得到關於ReferenceTable overflow
的錯誤。我知道這與在本機代碼中不釋放JNI/Java項目有關。我需要釋放我修改並返回的JNI數組嗎?
我以爲我不需要釋放任何東西,因爲我使用給定的jfloatarray
,替換它的值,然後再次返回原始的JNI對象。我認爲這會讓JNI釋放這個對象本身,因爲它只是取回它。
JNI代碼:
extern "C"
JNIEXPORT jfloatArray JNICALL Java_methodname(
JNIEnv *env, jobject thiz, jfloatArray data)
{
float* nativeValues = (float *)env->GetFloatArrayElements(data, 0);
doSomething(nativeValues);//overwrite the values with new values
return data;
}
JNI錯誤(安卓2.3):
11-28 15:00:58.069: W/dalvikvm(833): ReferenceTable overflow (max=1024)
11-28 15:00:58.069: W/dalvikvm(833): Last 10 entries in JNI pinned array reference table:
11-28 15:00:58.069: W/dalvikvm(833): 1014: 0x405f3750 cls=[F (20 bytes)
11-28 15:00:58.069: W/dalvikvm(833): 1015: 0x405f37b0 cls=[F (20 bytes)
11-28 15:00:58.069: W/dalvikvm(833): 1016: 0x405f3810 cls=[F (20 bytes)
11-28 15:00:58.069: W/dalvikvm(833): 1017: 0x405f3870 cls=[F (20 bytes)
11-28 15:00:58.069: W/dalvikvm(833): 1018: 0x405f38d0 cls=[F (20 bytes)
11-28 15:00:58.069: W/dalvikvm(833): 1019: 0x405f3930 cls=[F (20 bytes)
11-28 15:00:58.069: W/dalvikvm(833): 1020: 0x405f3990 cls=[F (20 bytes)
11-28 15:00:58.069: W/dalvikvm(833): 1021: 0x405f39f0 cls=[F (20 bytes)
11-28 15:00:58.069: W/dalvikvm(833): 1022: 0x405f3a50 cls=[F (20 bytes)
11-28 15:00:58.069: W/dalvikvm(833): 1023: 0x405f3ab0 cls=[F (20 bytes)
11-28 15:00:58.074: W/dalvikvm(833): JNI pinned array reference table summary (1024 entries):
11-28 15:00:58.074: W/dalvikvm(833): 1020 of [F 20B (1020 unique)
11-28 15:00:58.074: W/dalvikvm(833): 4 of [F 28B (4 unique)
11-28 15:00:58.074: W/dalvikvm(833): Memory held directly by tracked refs is 20512 bytes
11-28 15:00:58.074: E/dalvikvm(833): Failed adding to JNI pinned array ref table (1024 entries)
我該怎麼辦?將數據值複製到新的float*
並創建一個新的jfloatarray
?
這是否仍然允許我返回'data'? – Peterdk
主要思想:您應該通知JVM您已經使用陣列工作,並且可以在需要時使用GC釋放它。您可以使用'mode'參數指定發佈類型: 0 - 拷貝回內容並釋放elems緩衝區 JNI_COMMIT - 拷貝回內容但不釋放elems緩衝區 JNI_ABORT - 釋放緩衝區而不復制可能的更改 – maximus