Android crash with JNI ERROR local reference table overflow

Hi All

On some devices our app crash at startup with the following error (but on same device owned by other people, no problem) :
Abort message: ‘art/runtime/indirect_reference_table.cc:113] JNI ERROR (app bug): local reference table overflow (max=512)’

Here the full dump from Android developer console :

Build fingerprint: ‘htc/himauhl_htc_europe/htc_himauhl:5.0.2/LRX22G/549783.8:user/release-keys’
Revision: ‘0’
ABI: ‘arm’
pid: 18758, tid: 18782, name: UnityMain >>> <<<
signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
Abort message: ‘art/runtime/indirect_reference_table.cc:113] JNI ERROR (app bug): local reference table overflow (max=512)’
r0 00000000 r1 0000495e r2 00000006 r3 00000000
r4 def2ddd8 r5 00000006 r6 00000016 r7 0000010c
r8 00000000 r9 ab41da58 sl ac29e960 fp 0000037b
ip 0000495e sp def2d8f8 lr f70a2999 pc f70c8c54 cpsr 60070010

backtrace:
#00 pc 00039c54 /system/lib/libc.so (tgkill+12)
#01 pc 00013995 /system/lib/libc.so (pthread_kill+52)
#02 pc 000145b3 /system/lib/libc.so (raise+10)
#03 pc 00010eb9 /system/lib/libc.so (__libc_android_abort+36)
#04 pc 0000f408 /system/lib/libc.so (abort+4)
#05 pc 0021aa15 /system/lib/libart.so (art::Runtime::Abort()+160)
#06 pc 000a5eb7 /system/lib/libart.so (art::LogMessage::~LogMessage()+1322)
#07 pc 001559af /system/lib/libart.so (art::IndirectReferenceTable::Add(unsigned int, art::mirror::Object*)+518)
#08 pc 001d14f7 /system/lib/libart.so (art::JNI::FindClass(_JNIEnv*, char const*)+494)
#09 pc 003e1ef0 /data/app/-1/lib/arm/libunity.so
#10 pc 003de1bc /data/app/-1/lib/arm/libunity.so
#11 pc 005bf6f3 /data/dalvik-cache/arm/data@app@-1@base.apk@classes.dex

Any advice ?
help will be highly appreciated !

Unity 4.6.5f1
Facebook Plugin
Devices : Samsung Galaxy S5 , HTC One M9 (htc_himauhl)
Android 5.0

Hello I have the same issue with my application, I’ve understand that the problem is that we are allocating too many times the same object in a loop and JNI accept a maximum of 512 allocation of local reference. I use a coroutine in which an array is assigned each loop and I’m searching for a method to clear the previous allocation.

have you already managed the problem? could you help me?

Could you show how your coroutine looks like?

I don’t think that it could help, because I’m using a Inertial sensors for multiple bluetooth communication in streaming for android. I’m using a plugin for BT communication in Android using Unity by techTweaking (you can find it in asset store). I need to be fast so firstly i’ve used a coroutine in my main and now I’m trying with fixed update. My problem is, apparently, that the assignation blockReceived = device.read(); has too many local reference (more or less 500 before crash). Do you know a conceptual reason for this? how could i clear the content in my local reference map?

The conceptual reason is that JNI limits the number of local references and you have to either push/pop frames or delete local references once you no longer need them to avoid overflow.
For the later, we do have an API:
https://docs.unity3d.com/ScriptReference/AndroidJNI.DeleteLocalRef.html

However, with knowing what AndroidJava/JNI APIs you are using I can’t help you more. You mentioned something about putting elements into array. Could you paste that piece of code?