问题遇到的现象和发生背景
Android项目中包含JNI,运行时间长的话会报错:JNI ERROR (app bug): global reference table overflow (max=51200)
然后程序崩溃,请问各位这个应该怎么解决呢?
问题相关代码,请勿粘贴截图
#ifdef ANDROID
jobject NativeByteBuffer::getJavaByteBuffer() {
if (javaByteBuffer == nullptr && javaVm != nullptr) {
JNIEnv *env = 0;
if (javaVm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
if (LOGS_ENABLED) DEBUG_E("can't get jnienv");
exit(1);
}
javaByteBuffer = env->NewDirectByteBuffer(buffer, _capacity);
if (javaByteBuffer == nullptr) {
if (LOGS_ENABLED) DEBUG_E("can't allocate NativeByteBuffer buffer");
exit(1);
}
jobject globalRef = env->NewGlobalRef(javaByteBuffer);
env->DeleteLocalRef(javaByteBuffer);
javaByteBuffer = globalRef;
}
return javaByteBuffer;
}
运行结果及报错内容
04-27 13:09:01.659 2924 3052 F art : art/runtime/indirect_reference_table.cc:209] JNI ERROR (app bug): global reference table overflow (max=51200)
04-27 13:09:01.659 2924 3052 F art : art/runtime/indirect_reference_table.cc:209] global reference table dump:
04-27 13:09:01.659 2924 3052 F art : art/runtime/indirect_reference_table.cc:209] Last 10 entries (of 51200):
04-27 13:09:01.659 2924 3052 F art : art/runtime/indirect_reference_table.cc:209] 51199: 0x1654de40 java.nio.DirectByteBuffer
04-27 13:09:01.659 2924 3052 F art : art/runtime/indirect_reference_table.cc:209] 51198: 0x1654de00 java.nio.DirectByteBuffer
04-27 13:09:01.659 2924 3052 F art : art/runtime/indirect_reference_table.cc:209] 51197: 0x1654dc40 java.nio.DirectByteBuffer
04-27 13:09:01.659 2924 3052 F art : art/runtime/indirect_reference_table.cc:209] 51196: 0x1654db80 java.nio.DirectByteBuffer
04-27 13:09:01.659 2924 3052 F art : art/runtime/indirect_reference_table.cc:209] 51195: 0x16097d60 org.telegram.tgnet.ConnectionsManager$$ExternalSyntheticLambda13
04-27 13:09:01.659 2924 3052 F art : art/runtime/indirect_reference_table.cc:209] 51194: 0x1654d880 java.nio.DirectByteBuffer
04-27 13:09:01.659 2924 3052 F art : art/runtime/indirect_reference_table.cc:209] 51193: 0x1654d840 java.nio.DirectByteBuffer
04-27 13:09:01.659 2924 3052 F art : art/runtime/indirect_reference_table.cc:209] 51192: 0x1654d700 java.nio.DirectByteBuffer
04-27 13:09:01.659 2924 3052 F art : art/runtime/indirect_reference_table.cc:209] 51191: 0x1654d6c0 java.nio.DirectByteBuffer
04-27 13:09:01.659 2924 3052 F art : art/runtime/indirect_reference_table.cc:209] 51190: 0x1654d580 java.nio.DirectByteBuffer