participate


Java Native Interface (JNI) - How to unload JNI native library
<<   Back to Forum  |   Give us Feedback
10 Duke Stars available
This topic has 6 replies on 1 page.
hzhangsm
Posts:2
Registered: 2/21/02
How to unload JNI native library   
Jul 31, 2002 9:34 AM

 
I am working on a java console app that uses a native library thru JNI. I wonder when this native library will be unloaded? Is there anyway to control the unloading?
 
daney
Posts:1,226
Registered: 7/18/97
Re: How to unload JNI native library   
Jul 31, 2002 9:55 AM (reply 1 of 6)  (In reply to original post )

 
It will be unloaded when the ClassLoader of the Class that loaded it is unloaded.
 
jschell
Posts:36,985
Registered: 11/3/97
Re: How to unload JNI native library   
Jul 31, 2002 4:16 PM (reply 2 of 6)  (In reply to #1 )

 
Which means that if you are using the default class loader that it will never be unloaded.
 
rvcx
Posts:14
Registered: 8/12/02
Re: How to unload JNI native library   
Aug 13, 2002 8:11 AM (reply 3 of 6)  (In reply to #1 )

 
It will be unloaded when the ClassLoader of the Class
that loaded it is unloaded.

As has been mentioned in a few scattered places, in reality it's a bit more complex than this.

First, what matters is when the "class" which loaded the libary is unloaded, which may happen when the loader object which created it gets garbage-collected. (Internally I believe this is actually because the loader object holds a reference to the "class", which is a Java object in its own right.) Note that the class of the loader may remain loaded, which is actually very likely since that loader was probably loaded by the default loader object, which will probably not be garbage collected during normal execution.

Also note that unless you call System.runFinalizerOnExit(true) (which is not recommended) you are not guaranteed that the loader will ever be finalized, so you are not guaranteed that your JNUI_onUnload routine will ever be called. If you really really need something to happen in C before shutdown, the recommendation I've seen is to add a call to a native method using Runtime.addShutdownHook().
 
Luzius313
Posts:5
Registered: 4/30/07
Re: How to unload JNI native library   
Apr 30, 2007 8:49 AM (reply 4 of 6)  (In reply to original post )

 
Actually, there is a way to manually unload the native libraries. However, it is extremely ugly and breaks as soon as sun decides to change the relevant ClassLoader internals. You can do it with the following code snippet (assuming that the calling class has been loaded by the class loader whose native libraries you wish to unload). Don't use the libraries that have loaded the native libraries in the current context any more after you have unloaded them!

public void unloadNativeLibs() throws Throwable {
ClassLoader classLoader = this.getClass().getClassLoader();
Field field = ClassLoader.class.getDeclaredField("nativeLibraries");
field.setAccessible(true);
Vector libs = (Vector) field.get(classLoader);
for (Object o : libs) {
Method finalize = o.getClass().getDeclaredMethod("finalize", new Class[0]);
finalize.setAccessible(true);
finalize.invoke(o, new Object[0]);
}
}
 
jschell
Posts:36,985
Registered: 11/3/97
Re: How to unload JNI native library   
Apr 30, 2007 2:32 PM (reply 5 of 6)  (In reply to #4 )

 
Actually, there is a way to manually unload the
native libraries. However, it is extremely ugly and
breaks as soon as sun decides to change the relevant
ClassLoader internals. You can do it with the
following code snippet (assuming that the calling
class has been loaded by the class loader whose
native libraries you wish to unload). Don't use the
libraries that have loaded the native libraries in
the current context any more after you have unloaded
them!

(Interesting - providing bad advice in multiple old threads.)

That of course is extremely unsafe.

While the class loader and gc methodology is completely safe.
 
jschell
Posts:36,985
Registered: 11/3/97
Re: How to unload JNI native library   
Apr 30, 2007 2:34 PM (reply 6 of 6)  (In reply to #4 )

 
Actually, there is a way to manually unload the
native libraries.

I suppose it might be interesting to see if the VM cores or if it simply continues with the dlls loaded and with no possibility of ever reloading them.
 
This topic has 6 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 29
  • Guests : 132

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums