In jni we have GetPrimitiveArra开发者_开发问答yElements
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
Regardless of whether the jobject
s 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 jobject
s 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.
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.
精彩评论