Issue
This is JNI's C++ code:
extern "C" JNIEXPORT jbyteArray JNICALL
Java_cn_rilled_encoder_JNIEncoder_encodeEncryptBuff(
JNIEnv* env,
jobject obj,
jbyteArray ucInBuffer,
jintArray unInbufferLen,
jintArray unOutbufferLen) {
jbyte *bytes;
bytes = env->GetByteArrayElements(ucInBuffer, 0);
int chars_len = env->GetArrayLength(ucInBuffer);
// chars = new unsigned char[chars_len];
unsigned char* chars = (unsigned char *) malloc(chars_len);
memset(chars,0,chars_len);
memcpy(chars, bytes, chars_len);
env->ReleaseByteArrayElements(ucInBuffer, bytes, 0);
int inLength = env->GetArrayLength(unInbufferLen);
int* inArray = env->GetIntArrayElements(unInbufferLen,0);
int outLength = env->GetArrayLength(unOutbufferLen);
int* outArray = env->GetIntArrayElements(unOutbufferLen,0);
unsigned char* encryptBuff = WCCEncode_Encrypt_Buff(chars, (unsigned int*)inArray, (unsigned int*)outArray);
jbyteArray bArray=env->NewByteArray(*outArray);
env->SetByteArrayRegion(bArray, 0, *outArray, (jbyte *)encryptBuff);
//env->DeleteLocalRef(bArray);
//env->ReleaseByteArrayElements(bArray, (jbyte *)encryptBuff, 0);
env->ReleaseIntArrayElements(unInbufferLen, inArray, NULL);
env->ReleaseIntArrayElements(unOutbufferLen, outArray, NULL);
free(chars);
return bArray;
}
This is JNI's Java code:
public class JNIEncoder {
static {
System.loadLibrary("native-lib");
}
public static native byte[] encodeEncryptBuff(byte[] ucInBuffer, int[] unInbufferLen, int[] unOutbufferLen);
}
Call it in java thread:
byte[] result = JNIEncoder.encodeEncryptBuff(content, new int[]{content.length}, outLength);
memory usage:
memeory usage after freeing chars:
Solution
I solved the problem with delete encryptBuff
.
New Code
...
unsigned char* chars = (unsigned char *) malloc(chars_len);
...
free(chars);
delete encryptBuff;
return bArray;
New memory usage:
Answered By - Thomas Lee
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.