Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

NewStringUTF and references from calls to java

843829Aug 5 2004 — edited Aug 6 2004
Hi,

Before someone tells me to do a search first, believe me: I have! I just still dont have a definite answer :)

I have a program that might be suffering from a memory leak, the non-JNI part of the program is large but extremelly simple so i think im doing something wrong with regards to releasing JNI resources.

For example, consider this:
...
char* str = new char[100];
jstring jstr = env->NewStringUTF(str);
env->DeleteLocalRef(jstr);
...

Should i clean up str manually using delete[] str or will the JVM GC take care of this? Is DeleteLocalRef the correct way to clean up after NewStringUTF? Will the new jstring be a copy or a pointer to the native string?

A similar situation arises with GetStringUTFChars:

...
char* str = env->GetStringUTFChars(someJstring,NULL);
env->ReleaseStringUTFChars(someJstring,str);
...
Should i do something about str, or has it already been deleted/cleaned up? And does this behavior depend on the isCopy return value?

What about stuff returned from calling java method. Say i from java call a native method which in its operation calls a Java method which returns some object. I assume this will keep the returned object from being gc'ed until the reference i have is released, but is the returned value a localref which will disappear once my JNI method returns?

I would really appreciate some definite answers to this, ESPECIALLY the first question.

thanks for reading
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 3 2004
Added on Aug 5 2004
2 comments
2,888 views