@@ -140,6 +140,31 @@ public NativeInterface(AndroidJavaObject config)
140140 }
141141 }
142142
143+ /**
144+ * Pushes a local JNI frame with 128 capacity. This avoids the reference table
145+ * being exceeded, which can happen on some lower-end Android devices in extreme conditions
146+ * (e.g. Nexus 7 running Android 6). This is likely due to AndroidJavaObject
147+ * not deleting local references immediately.
148+ *
149+ * If this call is unsuccessful it indicates the device is low on memory so the caller should no-op.
150+ * https://docs.unity3d.com/ScriptReference/AndroidJNI.PopLocalFrame.html
151+ */
152+ private bool PushLocalFrame ( ) {
153+ if ( AndroidJNI . PushLocalFrame ( 128 ) != 0 ) {
154+ AndroidJNI . ExceptionClear ( ) ; // clear pending OutOfMemoryError.
155+ return false ;
156+ }
157+ return true ;
158+ }
159+
160+ /**
161+ * Pops the local JNI frame, freeing any references in the table.
162+ * https://docs.unity3d.com/ScriptReference/AndroidJNI.PopLocalFrame.html
163+ */
164+ private void PopLocalFrame ( ) {
165+ AndroidJNI . PopLocalFrame ( System . IntPtr . Zero ) ;
166+ }
167+
143168 public string GetAppVersion ( ) {
144169 return CallNativeStringMethod ( "getAppVersion" , "()Ljava/lang/String;" , new object [ ] { } ) ;
145170 }
@@ -287,10 +312,13 @@ public void LeaveBreadcrumb(string name, string type, IDictionary<string, string
287312 if ( ! isAttached ) {
288313 AndroidJNI . AttachCurrentThread ( ) ;
289314 }
290- using ( AndroidJavaObject map = BuildJavaMapDisposable ( metadata ) )
291- {
292- CallNativeVoidMethod ( "leaveBreadcrumb" , "(Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;)V" ,
293- new object [ ] { name , type , map } ) ;
315+ if ( PushLocalFrame ( ) ) {
316+ using ( AndroidJavaObject map = BuildJavaMapDisposable ( metadata ) )
317+ {
318+ CallNativeVoidMethod ( "leaveBreadcrumb" , "(Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;)V" ,
319+ new object [ ] { name , type , map } ) ;
320+ }
321+ PopLocalFrame ( ) ;
294322 }
295323 if ( ! isAttached ) {
296324 AndroidJNI . DetachCurrentThread ( ) ;
0 commit comments