On Jul 26, 2007, at 10:11 AM, Christine Grudecki wrote:
Currently, I'm opening and using the library as per the documentation.
However, my server is crashing after a few iterations of my program
because
there is no way to close the library after opening it during each
iteration
(it runs on a scheduler every 10 minutes). I think I need to be
using the
NativeLibrary class since it has the Finalize method, but I can't
find any
documentation or examples of how to use it instead of the Library
interface.
Can I use the Library interface in conjunction with the
NativeLibrary class?
Or are they mutually exclusive? How do I convert the example to use
Here are snippets of my code:
public interface MyLibrary extends Library {
MyLibrary INSTANCE = (MyLibrary)
Native.loadLibrary("/usr/local/lib/MyLibrary.so", MyLibrary.class);
This pattern should only be used if you are *not* concerned about the
library being closed and disposed. When you use the singleton
INSTANCE pattern, the library will remain opened until the MyLibrary
interface class object gets GC'd.
In order to enable GC of the library as soon as possible, use a local
variable instead of a static instance to hold the result of
Native.loadLibrary. You shouldn't need to use NativeLibrary
directly; the InvocationHandler for your library interface instance
will hold the only reference to the NativeLibrary (the NativeLibrary
cache uses weak references), so when the library interface gets GC'd,
the corresponding NativeLibrary will also be disposed.