sedj
Programmer
- Aug 6, 2002
- 5,610
Hi,
I've got a .so C library on which is called via JNI. This takes a few parameters, and creates a char* which points to about 300K ish of allocated memory (its basically a file). I then create a jbyteArray from this char*, copying in the contents. I then free the char*. This jbyteArray is then returned to the calling Java method.
However, there appears to be a substantial memory leak somewhere, and I don't believe it is within the native C code.
Does anyone have any idea how JNI frees a pointer to a byte array (the jbyteArray) after the JNI call is finished, and the calling Java method returns ?
The Java code is basically doing :
and the native C is basically :
Any insight would be cool !
--------------------------------------------------
Free Database Connection Pooling Software
I've got a .so C library on which is called via JNI. This takes a few parameters, and creates a char* which points to about 300K ish of allocated memory (its basically a file). I then create a jbyteArray from this char*, copying in the contents. I then free the char*. This jbyteArray is then returned to the calling Java method.
However, there appears to be a substantial memory leak somewhere, and I don't believe it is within the native C code.
Does anyone have any idea how JNI frees a pointer to a byte array (the jbyteArray) after the JNI call is finished, and the calling Java method returns ?
The Java code is basically doing :
Code:
public void doSomething(OutputStream os) {
byte[] bytes = myNativeMethod();
OutputStream os = ...
os.write(bytes);
os.flush();
os.close();
bytes = null;
}
and the native C is basically :
Code:
JNIEXPORT jbyteArray JNICALL myClass_nativeMethod((JNIEnv* env, jobject obj, ...) {
char *myPtr;
jbyteArray jb;
int size;
myPtr = make_me_a_big_300K_pointer(&size);
jb = (*env)->NewByteArray(env, size);
(*env)->SetByteArrayRegion(env, jb, 0, size, (jbyte*)myPtr);
free(myPtr);
return jb;
}
Any insight would be cool !
--------------------------------------------------
Free Database Connection Pooling Software