JNI_OnUnload not called

C

castillo.bryan

I have some JNI code which uses JNI_OnLoad to cache a pointer to the
JVM, found classes and method ids. I have used the JNI code with IBM's
and Sun's 1.4.2 JDK's on windows and linux. I wrote a JNI_OnUnload
function to delete the global refs to these classes etc... I noticed
that the unload function is never called. Its not much of a problem in
my environment since it usually means the JVM is shutting down anyway.
But I'm still curious why it isn't called. The documentation says that
both functions are optional for a JVM to implement. Is there no real
need to implement JNI_OnUnload?

Anyway this is how I declare the function

JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
/* code..... */
}

Am I doing anything wrong or is it just not implemented on the SUN and
IBM JDK's?
 
G

Gordon Beaton

I have some JNI code which uses JNI_OnLoad to cache a pointer to the
JVM, found classes and method ids. I have used the JNI code with
IBM's and Sun's 1.4.2 JDK's on windows and linux. I wrote a
JNI_OnUnload function to delete the global refs to these classes
etc... I noticed that the unload function is never called.

According to the JNI documentation, it will be called when the
classloader that loaded the libarary is garbage collected.

If you are holding references to the classloader, any of the classes
it loaded, or any of their objects, then you are preventing the
classloader from being garbage collected and JNI_OnUnload won't be
called.

That said, there are no guarantees given about when gc will collect
the classloader (or anything else for that matter), even if there are
no live references preventing it.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,052
Members
48,769
Latest member
Clifft

Latest Threads

Top