Search code examples
javajava-native-interface

Force JNI Get<type>ArrayElements to return a pointer


in the JNI method Get(Type)ArrayElements javadoc, it is written

"the returned array may be a copy of the Java array".

Therefore, this method returns either a pointer or a copy of the Java array. I made a simple test in my code, and indeed, this method returns a copy. Except that I wish to use native methods to speed-up huge images processing, therefore process BIG arrays. But if using a native code means duplicate my arrays, the performances will deeply suffer and I can lack in RAM.

So my question is: is there a way to force this method to return a pointer instead of a copy? Thank you for your help.


Solution

  • No, you cannot force non-copy. You can encourage in-place behavior by using instead GetPrimitiveArrayCritical() with the corresponding Release. Read documentation carefully because use of this function imposes significant limitations to what you can do, in addition to usual JNI limitations.