Reputation: 79595
Let's say I allocate a string using GetStringUTFChars()
, but then I don't release it:
JNIEXPORT void JNICALL Java_Sample1_nativeFunc
(JNIEnv *env, jclass, jstring s)
{
const char *utf = env->GetStringUTFChars(s, 0);
// env->ReleaseStringUTFChars(s, buf);
}
Would the string be automatically released at the end of the JNI call? That is, would the memory leak persist only during the JNI call, or would it remain afterwards, as well?
I'm asking because it would be such an easy optimization to implement that I can't believe a JVM implementation would not do it - it could keep track of any memory that needs to be released at the end of the JNI call, and release it automatically if the programmer failed to do so.
Does it depend on the JVM implementation? If so, I'm asking about Oracle's JVM. I looked at Should you call ReleaseStringUTFChars if GetStringUTFChars returned a copy?, and while it claims you should always call ReleaseStringUTFChars()
, it doesn't explicitly say that not doing so will create a memory leak that persists for the duration of the program.
Upvotes: 2
Views: 1207
Reputation: 30876
I looked at 'Should you call ReleaseStringUTFChars if GetStringUTFChars returned a copy?', and while it claims you should always call ReleaseStringUTFChars(), it doesn't explicitly say that not doing so will create a memory leak that persists for the duration of the program.
I would interpret that to mean that if ReleaseStringUTFChars()
is not called, then the memory won't be reclaimed. JNI can't automatically reclaim the memory, because your code might have retained a pointer to it (perhaps for use in the next invocation). It's not necessary to call the release() from the same stack frame as the get(), but you will want to arrange that it is called at some point (unless you know you're about to exit).
Upvotes: 1
Reputation: 310985
No. Local object rferences references are released automatically. Nothing else.
Upvotes: 0