從內存中釋放 jni 引用 (Freeing jni references from memory)


問題描述

從內存中釋放 jni 引用 (Freeing jni references from memory)

I create a byte array in java and pass it by reference to the jni function. This I do in a loop and and sometimes get a out of memory error in the jni. I wanted to know if java automatically frees the array on every iteration or since it is passed to the jni function, it doesn't ??

JNI Code     (bOldArray is the java byte array that i pass to jni as an argument)

len = (*env)->GetArrayLength(env,bOldArray);
char *oldBuff = (char *)calloc(sizeof(char),MAX_SIZE);
jbyte* bytes = (*env)->GetByteArrayElements(env,bOldArray,0);
memcpy(oldBuff,bytes,len);
(*env)->ReleaseByteArrayElements(env,bOldArray,(jbyte *)bytes,0);

參考解法

方法 1:

you have 2 buffers here, one handed from your java code (bOldArray)and the local buffer (oldbuff) which you allocated in line 2. 

in fact, you might have more buffers, because

(*env)->GetArrayLength 

with almost certainly makes a unmovable copy of the memory (needed for c-pointer-access), which holds the array in you java code and with 

(*env)->ReleaseByteArrayElements(env,bOldArray,(jbyte *)bytes,0);

this memory is copied back onto the memory of your java array (check documentation of the last argument of ReleaseByteArrayElements)

but about you problem: you should free oldBuff too.

free(oldBuff);

else the VM does free your c-copy of the java array but not the self-allocated part directly (it might do thiz later due to object-lifetime and garbage collection, but this is unpredictable and therefore the out-of-memory error is also unpredictable)

to avoid the java-c-copy mechanism (speeds up performance) use a shared/static buffer like ByteBuffer

方法 2:

If you are using GetByteArrayElements you have to call ReleaseByteArrayElements after you are done with the array in JNI, because the JVM will prevent the freeing of this array in java until you do so. Please post the code to get a clearer idea

(by VBKmojjjbobby)

參考文件

  1. Freeing jni references from memory (CC BY-SA 3.0/4.0)

#bytearray #android-ndk #java-native-interface






相關問題

更改創建 XML 閱讀器時使用的 XmlDictionaryReaderQuotas 對象的 MaxArrayLength 屬性 (Changing the MaxArrayLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader)

將文件讀入 4 個字節的 ByteArrays (Read file into ByteArrays of 4 bytes)

使用套接字 (C#) 進行文件傳輸 - 接收到的文件不包含完整數據 (File transfer using sockets (C#) - received file doesn't contain full data)

將 Java 字節讀取器轉換為 InputStream (Converting a Java byte reader to an InputStream)

用 bigInteger 和其他方法重建字節數組 (Rebuild byte array with bigInteger and other method)

如何將字節數組作為參數發送到 HTML.Action? (How to send byte array as paramater to HTML.Action?)

如何將存儲在字節數組中的圖像加載到 WebView? (How to load an image stored in a byte array to WebView?)

我希望將畫布保存為 mySql blob 字段中的 blob (I am looking to save a canvas as a blob in mySql blob field)

將字節數組的部分轉換為整數值 (Converting sections of byte arrays to integer values)

從內存中釋放 jni 引用 (Freeing jni references from memory)

為什麼 JPGEncoded bytearray 從 AS3 發送到 AMFPHP 會導致圖像無效? (Why is JPGEncoded bytearray sent from AS3 to AMFPHP resulting in an invalid image?)

Java:字節到浮點數/整數 (Java: Bytes to floats / ints)







留言討論