In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail what the analysis of the use of JNI is like, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Java NativeInterface (JNI) is a mixed programming method that uses the Java language and the native Cmax programming language. It allows Java code running in the Java Virtual Machine (VM) to interoperate with applications and libraries written in other programming languages (such as C, C++ and assembly). It supports loading code from dynamic link libraries and can use the efficient features of Cinning +.
Local reference and Global reference in JNI
Local reference (localreference) and global reference (globalreference) are important concepts in JNI, which are used to implement references to JNI object types. All Java objects returned by the JNI function are local references, and JNI allows global references to be created from local references. The local reference is valid during the invocation of the local method and is automatically released after the method returns; the global reference remains valid until it is explicitly released.
Therefore, the following considerations should be taken into account when actually using JNI:
1. The creation of a global reference NewGlobalRef (JNIEnv*,jobject) must be accompanied by the deletion of it DeleteGlobalRef (JNIEnv*,jobject), otherwise it will cause a memory leak and may lead to OutOfMemoryException
two。 Although local references are automatically released when the method returns, it prevents gc from collecting garbage objects, and the method's access to java objects creates a large number of local references. Even if the objects are no longer used, VM still needs space to track references, which may cause insufficient memory in the system.
During each local method call, VM creates a local reference table to map local references to java objects, and the JNI local reference table does not detect and collapse duplicate references. In DalivkVM, the maximum number of JNI local reference tables is 512, and loops or frequent string operations can easily lead to local reference table overflows. In these cases, you need to delete the local reference manually using DeleteLocalRef (JNIEnv*,jobject).
There are also the following interfaces for local reference operations:
EnsureLocalCapacity (JNIEnv*, jint)
Determines whether at least a given number of local references can be created in the current thread. The command line argument-verbose:jni during debugging can also display a warning message that too many local references are created.
PushLocalFrame (JNIEnv*, jint)
Create a new local reference frame and confirm that at least a given number of local references can be created.
PopLocalFrame (JNIEnv*,jobject)
Releases all current local references and returns the specified local reference.
NewLocalRef (JNIEnv *, jobject ref)
Creates a new local reference that references the specified object.
3. Because the local reference is released at the end of the call, the local reference cannot be saved with a global variable, otherwise it will crash due to the access wild pointer.
In addition to local variant references and global references, JNI also has weak global references (Weak Global References). It is a global reference that can be cleaned up by gc, and the underlying object referenced by it may be released while gc is running, when the reference is left empty. And although IsSameObject can be used to determine whether a weak global reference references an object that has been released, it does not prevent the object from being released. Therefore, it is not safe to call weak global references. When our native code no longer needs a weak global reference, we should also call DeleteWeakGlobalRef to release it. If we do not call this function manually to release the object pointed to, JVM will still reclaim the object pointed to by the weak reference, but the memory occupied by the weak reference itself in the reference table will never be reclaimed.
Exception handling in JNI
In the implementation of local method calls, the return of each JNI function call should be checked, handled, cleared of exceptions, and then other JNI function calls should be made, otherwise it will produce unpredictable results. Once an exception occurs, return immediately and let the caller handle the exception. Or call ExceptionClear () to clear the exception and then execute your own exception handling code. The JNI interface ExceptionCheck (JNIEnv*) can determine whether an exception has occurred and does not generate new local references. Interface ExceptionOccurred (JNIEnv*) can create a local reference to return an exception object.
It is worth noting that when an exception occurs, the native code must handle the exception before performing other JNI calls. Only the specified API can be called in case of exception suspension to handle exceptions and release resources:
ExceptionOccurred () ExceptionDescribe () ExceptionClear () ExceptionCheck () ReleaseStringChars () ReleaseStringUTFChars () ReleaseStringCritical () ReleaseArrayElements () ReleasePrimitiveArrayCritical () DeleteLocalRef () DeleteGlobalRef () DeleteWeakGlobalRef () MonitorExit () PushLocalFrame () PopLocalFrame ()
JNI builds a bridge between the VM running bytecode and the local platform specific code, and plays an important role in reusing the Candle code, increasing Java access to some low-level features of the operating system, and improving the running efficiency of the code. However, the communication and data exchange between the native code and the java code will also cause serious consequences such as virtual machine crash or memory leakage. Therefore, it is very important to deeply learn and understand the characteristics of JNI in the development process.
On the use of JNI analysis is what is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.