java - will cache jclass in native code prevent the class been unloaded -


according https://developer.android.com/training/articles/perf-jni.html:

the class references, field ids, , method ids guaranteed valid until class unloaded. classes unloaded if classes associated classloader can garbage collected

if keep global reference of class, guarantee class never been unloaded?

static jclass g_classmyclass;  jint jni_onload(javavm *pjvm, void *reserved) {     jnienv *penv;     if (pjvm->getenv((void**)&penv, jni_version_1_6) != jni_ok) {         return -1;     }      jclass classmyclass = penv->findclass("com/example/myclass");     g_classmyclass = (jclass)penv->newglobalref(classmyclass);      return jni_version_1_6; } 

imo, if use default stuff rather play class loaders, reference method or field of myclass other classes of application enough prevent unloading of myclass; not bother.

what can happen in practice process killed , restarted, , in case static variables set initial values (this why activities use bundles rather static variables pass data in future).


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -