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.