Android: JNI ERROR (application error): local link table overflow (max = 512)

I have an Android app that has its own code. Native code should get a specific value from java code; this value is updated regularly, so I need to get it when I need to use it. I use JNI to make a call from native code to Java code.

std::string val;
JNIEnv* env = JSC::Bindings::getJNIEnv();
jclass bridgeClass = env->FindClass("com.mypackage.MyClass");
jmethodID method = env->GetStaticMethodID(bridgeClass, "getVal", "()Ljava/lang/String;");
val = jstringToStdString(env, static_cast<jstring>(env->CallStaticObjectMethod(bridgeClass, method)));
env->DeleteLocalRef(bridgeClass);

I make this call very often (almost 100 times per minute), and I come across the following exception:

E/dalvikvm( 1063): JNI ERROR (app bug): local reference table overflow (max=512)
W/dalvikvm( 1063): JNI local reference table (0xcc8590) dump:
W/dalvikvm( 1063):   Last 10 entries (of 512):
W/dalvikvm( 1063):       511: 0x413c7e70 java.lang.String "ABC"
W/dalvikvm( 1063):       510: 0x40a39470 java.lang.Class<android.util.Log>
W/dalvikvm( 1063):       509: 0x413c8558 java.lang.String "9287391238192... (24 chars)
W/dalvikvm( 1063):       508: 0x413c8558 java.lang.String "8298731897198... (24 chars)
W/dalvikvm( 1063):       507: 0x413c8558 java.lang.String "1983918729387... (24 chars)
W/dalvikvm( 1063):       506: 0x413c8558 java.lang.String "9283719732827... (24 chars)
W/dalvikvm( 1063):       505: 0x413c8558 java.lang.String "1231219897173... (24 chars)
W/dalvikvm( 1063):       504: 0x413c8558 java.lang.String "8237330127537... (24 chars)
W/dalvikvm( 1063):       503: 0x413c8558 java.lang.String "1293657681298... (24 chars)
W/dalvikvm( 1063):       502: 0x413c8558 java.lang.String "1298753090172... (24 chars)
W/dalvikvm( 1063):   Summary:
W/dalvikvm( 1063):         2 of java.lang.Class (2 unique instances)
W/dalvikvm( 1063):       510 of java.lang.String (2 unique instances)
E/dalvikvm( 1063): Failed adding to JNI local ref table (has 512 entries)

All similar questions online have a common answer that you need to free up more resources. Can anyone tell me what other resources I can free in this case?

Thank.

+5
source share
1 answer

You need to remove the local ref to the value returned

env->CallStaticObjectMethod(bridgeClass, method)

:

jobject returnValue = env->CallStaticObjectMethod(bridgeClass, method);
// ...
env->DeleteLocalRef(returnValue);
+6

All Articles