Java JNI调用加载库

JPM*_*JPM 8 java java-native-interface loadlibrary

如果我有两个对编译的C代码进行本机调用的Java类并且我在另一个类中调用这两个类,它会影响内存吗?例如,我有A类和B类,同时调用本机函数.它们的设置如下:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

C类然后调用两个,因为它们都进行静态调用来加载库在C类中是否重要?或者让C类调用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();

    }
}
Run Code Online (Sandbox Code Playgroud)

And*_*mas 8

不,没关系.在同一个类加载器中多次调用loadLibrary()是无害的.

Runtime.loadLibrary(String)的文档,由System.loadLibrary(String)调用:

   If this method is called more than once with the same library name, 
   the second and subsequent calls are ignored.
Run Code Online (Sandbox Code Playgroud)