This content has been marked as final. Show 6 replies
The spec for JVM/TI GetThreadInfo() also mentions:
The object returned in the field thread_group of jvmtiThreadInfo is a
JNI local reference and must be managed. The object returned in
the field context_class_loader of jvmtiThreadInfo is a JNI local
reference and must be managed.
Since you are calling GetThreadInfo() in a tight loop, you are allocating
two JNI local references per iteration. Those will need to be freed so
you should follow the link to the JNI spec. I don't know if this will account
for all of your memory "leak", but it's a start.
Yeap, that was it. Fixing the code to
stopped the leaking. Thank you indeed for your help, I was sidetracked by numerous sources I found on the Internet, that never managed these objects (probably because they used a previous version of JVMTI that did not contain them in the corresponding struct).
jvmti->GetThreadInfo(myThread, &info); jni_env->DeleteWeakGlobalRef(info.thread_group); jni_env->DeleteWeakGlobalRef(info.context_class_loader); jvmti->Deallocate((unsigned char *)(info.name));
Just a clarification: those fields have always been present in the
Also, you used JNI DeleteWeakGlobalRef() and you probably should
have used JNI DeleteLocalRef().
The examples you found on the web might have been simple calls to
GetThreadInfo() that returned from the native code right away. In that
case, explicit management of the local JNI references is not needed.
See the following link:
for the gory details.
Thanks for the clarification, though I do not quite get when this management should take place explicitly by the programmer and when it is automatically done (your your link is broken and I couldn't make more out of it - could you plz post the correct one - searching for variations of it, I ended up in a free dating services site :)).
Meanwhile, regarding the weak references thing, using DeleteGlobalRef, as I initially did, resulted in the -Xcheck:jni complaining:
FATAL ERROR in native method: Invalid global JNI handle passed to DeleteGlobalRef
I really do not want to take any more of your time - just pointing to the link you mention is more than enough