Liệu nó có ảnh hưởng đến bộ nhớ nếu tôi có hai lớp Java có các cuộc gọi gốc tới mã C đã biên dịch và tôi gọi cả hai lớp đó trong một lớp khác không? Ví dụ tôi có Class A và Class B với cả hai cuộc gọi đến các hàm gốc. Chúng được thiết lập như sau:Java JNI gọi để tải thư viện
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;
}
}
Lớp C sau đó gọi cả hai vì cả hai thực hiện cuộc gọi tĩnh để tải thư viện sẽ quan trọng trong lớp C? Hoặc là nó tốt hơn để có Class C gọi 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();
}
}