• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with loadlibrary

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic