I'm not clear on what I think is supposed to be workaround for the
lack of NewWeakGlobalRef() support in Dalvik; dalvik/docs/jni-
tips.html states that java.io.FileDescriptor shows how to cache class
IDs, by making a static "nativeClassInit()" which is called in the
class static initializer, which will create a global reference to the
class on the native side.
As I understand it though, global references to a class prevent the
garbage collection of that class, and if the class is never garbage
collected then the classloader for that class will never be collected,
and the native lib will never be unloaded. Quoting
http://java.sun.com/docs/books/jni/html/other.html#30440 :
"We are now ready to explain why we cache the C class in a weak global
reference instead of a global reference. A global reference would keep
C alive, which in turn would keep C's class loader alive. Given that
the native library is associated with C's class loader L, the native
library would not be unloaded and JNI_OnUnload would not be called."
Looking at dalvik/libcore/luni/src/main/native/
java_io_FileDescriptor.c, it seems like that code is guilty of this
mistake, because it is creating a strong global reference which is
never deleted? But I must be missing something...