Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with loadlibrary  RSS feed

 
kirti parekh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Please assist ASAP!!!

Thanks,
Kirti
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

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.)
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!