10

Does it impact memory if I have two Java classes that have native calls to compiled C code and I call both those classes in another class? For instance I have Class A and Class B with both calls to native functions. They are setup like this:

public class A{
    // declare the native code function - must match ndkfoo.c
    static {
        System.loadLibrary("ndkfoo");
    }

    private static native double mathMethod();

    public A() {}

    public double getMath() {
          double dResult = 0;  
          dResult = mathMethod();
          return dResult;
    }
}


public class B{
    // declare the native code function - must match ndkfoo.c
    static {
        System.loadLibrary("ndkfoo");
    }

    private static native double nonMathMethod();

    public B() {}

    public double getNonMath() {
          double dResult = 0;  
          dResult = nonMathMethod();
          return dResult;
    }
}

Class C then calls both, since they both make a static call to load the library will that matter in class C? Or is it better to have Class C call System.loadLibrary(...?

public class C{
    // declare the native code function - must match ndkfoo.c
    //  So is it beter to declare loadLibrary here than in each individual class?
    //static {
    //  System.loadLibrary("ndkfoo");
    //}
    //

    public C() {}

    public static void main(String[] args) {
        A a = new A();
        B b = new B();
        double result = a.getMath() + b.getNonMath();

    }
}
JPM
  • 9,077
  • 13
  • 78
  • 137

4 Answers4

10

No, it doesn't matter. It's harmless to call loadLibrary() more than once in the same classloader.

From the documentation for Runtime.loadLibrary(String), which is called by System.loadLibrary(String):

   If this method is called more than once with the same library name, 
   the second and subsequent calls are ignored.
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
2

Its better to have the class which uses the library, load the library. If you have to caller load the library you make it possible to call the native methods without loading the library.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

Jni libs are dynamic libs. I'd think they'd have to be in order to be loaded by loadLibrary. One of the advantages of dynamic libraries is that if they are already loaded into memory, that copy gets used instead of being reloaded. So you can use the two loadlibrary calls.

The other issue is that if you put the loadlibrary call in class C, you've ruined the encapsulation of the other two classes. In any large project, someone is eventually going to call one of the native calls in class a or class b without going through class c. That will not work so well.

dar512
  • 862
  • 8
  • 18
0

Seems like and NdkFoo class would be prudent, and have every method be a native one. Then from A you could use

NdkFoo.getInstance().mathMethod();

and B could do

NdkFoo.getInstance().nonMathMethod();

It also makes creating the native library name consistent with the backing java class name.

Nate
  • 4,724
  • 1
  • 21
  • 9