Skip to content

Stop using JNIEnv.Call*Method() #1756

Open
@jonpryor

Description

@jonpryor

Steps to Reproduce

  1. Run a Xamarin.Android within Profiler and look at the allocations.
  2. Notice that many JniMethodInfo instances are being allocated -- many thousands, depending on project setup.
  3. Cry at all the allocations.

The cause of the allocations is that Android.Runtime.JNIEnv is now largely a "legacy" API, superseded by Java.Interop. In order to avoid JNIEnv binding duplication, most of the JNIEnv methods were updated to delegate to Java.Interop:

public static unsafe IntPtr CallObjectMethod (IntPtr jobject, IntPtr jmethod)
{
	return JniEnvironment.InstanceMethods.CallObjectMethod (new JniObjectReference (jobject), new JniMethodInfo (jmethod, isStatic: false)).Handle;
}

For any code using the legacy API, this can be expensive on the GC.

What we should do:

  1. Audit the xamarin-android codebase to ensure we're not using JNIEnv.Call*Method() within our own codebase.
  2. Strongly suggest that all Java Library Binding projects set $(AndroidCodegenTarget)=XAJavaInterop1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: BindingsIssues in Java Library Binding projects.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions