Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JNI, memory leak and JNI memory deallocation

Status
Not open for further replies.

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 :

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
 
AFAIK, you can only make direct references to memory from within the native code. Once you return from there, is the garbage collector the one who will take care of it.

I'm not sure if you need to call a release operation from your native code in your byte array.

HTH

Dian
 
Code:
public void doSomething(OutputStream os) 
{
  byte[] bytes = myNativeMethod();
// (...)
  bytes = null;
}
Since you leave the scope, where 'bytes' is defined, the nulling is perfect nonsense.


seeking a job as java-programmer in Berlin:
 
Cheers for that Stefan !!!
 
Diving on my mind (I used to work with JNI), I remember that, appart from getting and releasing memory from the C code (malloc, free and so on), you could also use JNI calls to do that.

Probably when you do a newByteArray that's what you're doing. Then, the free(Ptr) frees the memory for C code, but not for JNI linker, so maybe what you need is a ReleaseByteArrayElements before returning.

HTH.

Dian
 
Oops. Just found the memory leak in my C code. [blush]
Thanks for the replies guys !
 
What was it? I was wondering...

haslo@haslo.ch - www.haslo.ch​
 
In effect it was this :

Code:
AnImagePtr myPtr;
AnotherPtr anPtr;
FILE* in;

for (i = 0; i < something; i++) {
  in = fopen(someFiles[i], "r");
  myPtr = someFunction(in);
  anotherFunction(myPtr, anPtr);
  free(anPtr);
  // forgot to free myPtr
}

It was a bit harder to spot than that though, as there was a lot more code. I should stick with Java !!!
 
I think your problem is the variable naming. something, somefunction are a nono.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
That was pseudocode ;-) - and sedj's problem was the dangling myPtr, as he pointed out in his post.

haslo@haslo.ch - www.haslo.ch​
 
yes,yes just kidding. but in his first post he frees myptr and in the second he doesn't free myptr but anptr. I think poor old sedj is getting a bit confussed. 1nd I don't like posters then forget half of the question and then don't even give the answer.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
OK chrissie, calm down, calm down - its nearly the weekend.
 
<ironic>
Yes, I also don't like people like sedj that spend all day asking questions
</ironic>
 
I know, I'm such a mooch - its just "take take take" with me !!!



--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top