JNI: Overhead of moving references to Java objects inside native code?

I am studying JRE integration in a C ++ application through JNI.

What is the overhead of having a large number of references to Java objects in a C ++ application ( global references in JNI)?

Are there any problems that I should know about with this approach (other than the obvious, for example, when manually releasing links)?

+4
source share
2 answers

(a) the overhead is the same as if you were doing it with Java. You prevent garbage collection.

(b) Keeping object references in JNI calls can be fatal for the JVM if you don't do it right. You should carefully read the Global and Local Links section of the JNI specification. You should also consider using weak links instead of global ones.

+3
source

This is more of an opinion than an answer, but given that you have a choice, I highly recommend not using JNI and, rather, communicating between your C ++ application and the Java virtual machine using a different mechanism, such as Sockets or web services . If you get it right, the JNI solution will be much faster than any of these alternatives, but if performance is not critical, then my JNI experience is that it is best avoided.

As EJP (+1) correctly points out, if you do not properly manage your JNI Java objects, very bad things will happen - including the VM just dying. I also found that JNI code is very difficult to verify.

-2
source

All Articles