Hi All, In my Java Swing code(on the server side), I am calling System.loadLibrary() to call a C function. Whenever I want to save an object, I call the save() function, which in turn calls this loadLibrary()(non-static). Also, my application is experience inferior performance after this change implemented. My questions are: 1. Can calling System.loadLibrary() for every request degrade the application performance? 2. Should this method be necessarily called in a thread-safe method(currently that is not happening) 3. If loadLibrary() is a candidate for performance issue, then in order to resolve the issue should I: 3.1 Call this loadLibrary() only once.. i.e. a static block declaration once for the life of the application? 3.2. Convert this C function into Java executable code and use the same?
loadLibrary() should always be called in a static initializer block of the class that uses the native functions. This will ensure that it's called in the right context, at the right time, and only once. I really couldn't say what happens if you call it repeatedly on the Swing thread, but I'm sure it's not good, as you've observed. I've never seen code that didn't do things the recommended way (the way I describe above.)
Originally posted by Ernest Friedman-Hill: loadLibrary() should always be called in a static initializer block of the class that uses the native functions
I don't think that's true. Calling it in a static initialiser block is certainly the most common way to achieve loading at the right time, but it's not the only way.
One situation when it is not the right thing is when you are explicitly registering native functions, using registerNatives(). In that case, your native code loads the library, then finds the required exports within it. Then it passes those exports to registerNatives(), to connect them to the associated Java native methods. You never have to System.loadLibrary() at all.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
I brought this back from the farm where they grow the tiny ads: