I have the following JNI method that creates a collection of Java objects natively, and then return them to Java:
JNIEXPORT jobject JNICALL Java_com_test_myClass_myMethod(JNIEnv * env, jclass klass) {
jclass arrayClass = env->FindClass("java/util/ArrayList");
jmethodID initMethod = env->GetMethodID(arrayClass, "<init>", "()V");
jmethodID addMethod = env->GetMethodID(arrayClass, "add", "(Ljava/lang/Object;)Z");
jobject myArray = env->NewObject(arrayClass, initMethod);
env->CallBooleanMethod(myArray, addMethod, env->NewStringUTF("Hello"));
env->CallBooleanMethod(myArray, addMethod, env->NewStringUTF("World"));
return myArray;
}
Do I need to free the objects created in the native code, or is it done automatically by the GC? If I do, how do I do that, as I need to return it to Java?
JNI is the Java Native Interface. It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++).
A JNI library can easily cause the Java program to exceed its maximum specified heap.
The JNI header " jni. h " provided by JDK is available under the " <JAVA_HOME>\include " and " <JAVA_HOME>\include\win32 " (for Windows) or " <JAVA_HOME>\include\linux " (for Ubuntu) [Check Mac OS X] directories, where <JAVA_HOME> is your JDK installed directory (e.g., " c:\program files\java\jdk10.
You do not need to free the Java objects created in the native code. In fact, you cannot. The garbage collector may free the object when no further references remain.
Occasionally it is useful in native code to free references to Java objects. This can reduce memory requirements when the native code holds, but no longer needs, references to large objects or a large number of references.
From: "Global and local references" in the JNI specification.
In most cases, the programmer should rely on the VM to free all local references after the native method returns. However, there are times when the programmer should explicitly free a local reference. Consider, for example, the following situations:
- A native method accesses a large Java object, thereby creating a local reference to the Java object. The native method then performs additional computation before returning to the caller. The local reference to the large Java object will prevent the object from being garbage collected, even if the object is no longer used in the remainder of the computation.
- A native method creates a large number of local references, although not all of them are used at the same time. Since the VM needs a certain amount of space to keep track of a local reference, creating too many local references may cause the system to run out of memory. For example, a native method loops through a large array of objects, retrieves the elements as local references, and operates on one element at each iteration. After each iteration, the programmer no longer needs the local reference to the array element.
Additional detail was provided See "Freeing References" in the JNI Programmer's Guide.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With