4

I am trying to unload a dll in java. I have read this and this but it seems that you can not guarantee that the dll will actually be unloaded at a certain time. This is because System.gc() simply "asks kindly" for the garbage collector to run.

So here is a break down of the situation. I have a dll that provides some functionality via JNI. Lets call this dll MainDll. MainDll is loaded from a call to System.load("MainDll"). I need to be able to unload and load this dll on the fly.

Is it possible to create another dll that's sole purpose is to load and unload MainDll. Lets call this dll LoaderDll. I could then simple call System.load("LoaderDll") and have some native functions to load and unload MainDll. The reason for doing this, is I have access to functions on the native system that can load and unload the dll on the fly. The tricky part with this is, will I have still be able to access the native functions I have written in MainDll if it loaded from inside LoaderDll.

Sorry if this is a confusing question. It seems its a little difficult to explain.

Thanks

Community
  • 1
  • 1
user489041
  • 27,916
  • 55
  • 135
  • 204
  • What problem do you want to solve this way?(Could there be an alternative to deterministic loading and unloading, just asking to get a better idea) – josefx Nov 30 '10 at 15:50
  • Why do you need to be able to load/unload MainDLL on the fly? Normally the load/unload issue comes about in a development scenario, when the DLL is deployed as part of a build. Is this your situation, or do you have another reason? – Anon Nov 30 '10 at 15:58
  • Its a video conferencing application. There are a couple of reasons why I would like to unload it. After a call finishes, there are a lot of different things that need to be cleaned up. Something could go wrong. Say for example, if there is an issue in the H.323 stack. I need to be sure that I can unload and then reload the driver if it has been corrupted reliably or the application will not operate as expected. – user489041 Nov 30 '10 at 16:00

5 Answers5

2

Create a wrapper DLL that does the loading/unloading. Also have wrapper methods in the DLL that turn around and delegate the calls to the loaded MainDll DLL. This way your Java JNI code only knows about a single DLL. It can still request the unload [LoaderDll::unload()] which internally unloads the MainDll.

This should work as long as the methods/functions in LoaderDll can trigger a load of MainDll when they are called when MainDll is not currently loaded, assuming that is the desired behavior instead of throwing an exception/error.

One issue with this would be that LoaderDll would always be loaded.

Kelly S. French
  • 12,198
  • 10
  • 63
  • 93
1

Add a level of indirection.

Make your native methods call forwarding routines in LoaderDLL. The forwarding routines can use C facilities to forward the calls to code in mainDLL.

Darron
  • 21,309
  • 5
  • 49
  • 53
0

I haven't still encountered a situation where System.gc() not to trigger the garbage collector although it is just a hint. This tutorial actually helped me to do my work: Unload Java JNI DLL

johnny
  • 7
0

If you have a need to dynamically load and unload code, have you considered OSGi. This works in felix at least.

In Oracle/Sun's JDK, System.gc() will trigger a full gc (unless it has been turned off on the command line). It could be just a hint on other JVMs.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Based on your clarifying comments, I think the simplest approach would be to spawn a new JVM, whose sole responsibility is managing your DLL. Probably exposing an RMI interface to access those classes (although a simple stream may be sufficient).

Anon
  • 2,654
  • 16
  • 10