Should ReleaseStringUTFChars be called after GetStringUTFChars (when passing the char * to C function)?

Am I a little confused about the objects that are passed to c from java? Should they be deleted inside their own jni method or will they collect garbage when the method returns. For example:

if I have my own declaration in my java file public native printString(String msg); , and the native method uses const char *message = (jni_env)->GetStringUTFChars(msg, &iscopy); to get the c-style character array. Shoud I call (jni_env)->ReleaseStringUTFChars(msg, message); after doing all the material in your own method. If so, why is this needed? Why doesn't the Java runtime do this on behalf of the programmer? After the string has been declared and passed from the java environment.

+7
source share
1 answer

The Get Characters function returns characters in memory until the Release method is called. Java cannot collect garbage or otherwise move this data until it is certain that no one is using it.

The Java virtual machine cannot know anything about how long memory will be used when it leaves the Java virtual machine, so it requires manual notification that the memory has run out.

+5
source

All Articles