• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

static block and Loading of native library

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class that loads native library in static block. All other methods in the class are NOT static methods - so I need to instantiate the class to call methods on it.

------------------------------------------
class MyClass {

static {
System.loadLibrary("MyLibrary");
}

public native string method1();
}

calling program method:
-------------------------------------------
{
.....
MyClass myClass = new MyClass();
String str = myClass.method1();
myClass = null; //allow it to be garbage collected
......
}

calling program method2:

{
.....
MyClass myClass = new MyClass();
String str = myClass.method1();
myClass = null; //allow it to be garbage collected
......
}

-----------------------------------------------------

Please someone tell me if my understanding is wrong:

The native library will be loaded when the class will be instantiated for the first very time. Even if I set the instance to null and if the object really gets garbage collected, because the native library is loaded from a static block, it still remains loaded and next time I instantiate the class (in calling program method2), it won't load that library again - is this thought process right???

Thanks,
P.Ingle
 
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
The native library will be loaded before (possibly immediately before, but some time before) the class is instantiated, when the class is loaded. The library will never be loaded again...

... unless, of course, the class is loaded a second time by another class loader. What happens under these circumstances used to be murky, at best; I think they may have clarified the situation in Tiger. But in any case, the library will be loaded once, when the class is loaded, and instantiating objects will not cause it to be reloaded, regardless of garbage collection.
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic