Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jni- Releasing Object Arrays

In jni we have GetPrimitiveArrayElements functions to get pointer to the array elements on the heap and ReleasePrimitiveArrayElements to remove the local copy of the arrays.

however I am passing array of java objects to JNI.These array elements are iterated using GetObjectArrayElement function to local jobject.

But how can I remove the local reference of the jobject after processing of the array elements.

Thanks

like image 992
Sandeep Avatar asked Apr 14 '11 05:04

Sandeep


People also ask

What is JNI Jobject?

jobject thiz means the this in java class. Sometimes if you create a static native method like this. void Java_MyClass_method1 (JNIEnv *, jclass); jclass means the class itself. Follow this answer to receive notifications.

What is JNIEnv * env?

ThrowNew. jint ThrowNew(JNIEnv *env, jclass clazz, const char *message); Constructs an exception object from the specified class with the message specified by message and causes that exception to be thrown.

What is JNI reference?

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++).


2 Answers

Regardless of whether the jobjects you are grabbing were originally allocated on the Java side or by your JNI method, they will be handled by garbage collection as long as there are no lingering references to the objects. Therefore, if your local references to the jobjects are just local variables, they will disappear at the end of the function and your object will be eligible for garbage collection in the normal cause of events. If you retain a GlobalRef to the objects, then the object will still exist and the local reference will just disappear like any local variable that wasn't allocated heap space). If you retain a WeakRef, the object may be garbage collected, but if not, it remains valid for another JNI call. Retaining an ordinary local reference to a jobject across JNI calls is not valid.

Also, if you want to release your local reference right away and not wait (like if you were creating a ton of jobject references in a single function, just use the DeleteLocalRef(env, jobj); method of JNIEnv.

In any case, the documentation should tell you everything you need to know if I have made any mistakes.

like image 166
Nitrex88 Avatar answered Nov 01 '22 22:11

Nitrex88


You can either do nothing or call DeleteLocalRef().

From the JNI Specification Chapter 2:

Global and Local References

The JNI divides object references used by the native code into two categories: local and global references. Local references are valid for the duration of a native method call, and are automatically freed after the native method returns. Global references remain valid until they are explicitly freed.

Objects are passed to native methods as local references. All Java objects returned by JNI functions are local references. The JNI allows the programmer to create global references from local references. JNI functions that expect Java objects accept both global and local references. A native method may return a local or global reference to the VM as its result.

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.

The JNI allows the programmer to manually delete local references at any point within a native method. To ensure that programmers can manually free local references, JNI functions are not allowed to create extra local references, except for references they return as the result.

Local references are only valid in the thread in which they are created. The native code must not pass local references from one thread to another.

like image 1
user207421 Avatar answered Nov 01 '22 22:11

user207421