Improve Java performance with JNI technology
Capital University of Information College
A main factor in hindering Java to obtain a wide range of applications is the operational efficiency of the Java program. Java is a language between interpretation and compilation types, the same procedure, if implemented with compilation language C, and its running speed is more than doubled than Java. Java has a platform-independent, which makes people always use one of its main candidates when developing enterprise applications, but performance factors have greatly weakened its competitiveness. To this end, it is very important to improve the performance of Java. Problem The SUN and Java supporters have made many efforts to improve Java's running speed, most of which focuses on programming methods and mode selection. Due to the optimization of algorithms and design patterns, it is basically equally applicable to Java effective optimization algorithms and design patterns, and therefore does not fundamentally change the difference between Java programs and compilation languages in execution efficiency. JIT (Just In Time, timely compilation) technology is a better idea. Its basic principle is: First compile the Java source code into a platform-independent binary bytecode via the Java compiler. Then before the JAVA program is actually executed, the system compiles the Java bytecode as a localized machine code through the JIT compiler. Finally, the system performs localized machine code to save time for the word code. The advantage of this is that the performance of the Java program is greatly improved, and the time to load the loader; at the same time, the storage space is also saved because the results of the compilation are not saved during the program. The disadvantage is that since the JIT compiler wants to optimize all the code, it also takes a lot of time. Dynamic optimization technology is another attempt to improve Java performance. This technology tries to improve the performance of Java by using Java source program directly into machine code to make full use of Java dynamic compilation and static compilation techniques. This method converts the input Java source or bytecode to a highly optimized executable code and a dynamic library (Windows DLL file or Unix. SO file). This technology greatly enhances the performance of the program, but destroys Java portability. JNI technology actually, there is a technology that is usually ignored by us to a large extent, which is JNI (Java Native Interface, Java localization method). Adopting people using pure Java usually oppose the use of localized code, they think that calling C / C programs during Java programs will affect the portability and security of the program. Some people think that JNI is only a simple extension of past mixed programming technology, and the actual purpose is to make full use of a large number of original C libraries. In fact, we don't have to stick to rigorous platform independence limits, because JNI technology is used only for some code segments that seriously affect Java performance, which may only account for very little part of the source, so it is almost regardless of this part of the code in mainstream The workload of transplantation between platforms. At the same time, you don't have to worry too much about the type matching problem, we can completely control the code does not have this error. In addition, you don't have to worry about safety control, because the Java security model has expanded to allow non-system class loading and call local methods. According to Java specifications, starting from JDK 1. 2, FindClass will try to find class loaders associated with current local methods. If the platform-related code belongs to a system class, there is no need to involve any class loader; otherwise, the appropriate class loader will be called to load and link the named class. In other words, if the machine code generated directly in the C / C language directly in the Java program, the security of this part of the code is controlled by Java Virtual Machine. JNI Implementation Steps Write the approximate process of JNI code as shown below: JNI implementation flowchart 1. First prepare the Java source file that requires JNI functionality. Among them, the method of JNI implementation should be declared with a Native keyword. In this class, use the System. LoadLibrary () method to load the required dynamic link library.
The key code is as follows: //compute.java ... public class compute {public native double compute (double [] params); ... static {// call dynamic link library system. LoadLibrary ("Mathlib");} ...} 2 Compile the source file with the Java class compiler to binary bytecode files. Due to the Native keyword declaration, the compiler ignores the JNI method part without a code body. 3. Generate the header file of the relevant JNI method using javah -jni * .class. We can manually generate this file, but since the Java virtual machine is completed according to a certain naming specification, the hand-written header needs to be particularly careful. The header code generated by the above file is as follows: // compute. H ... extern "c" {jniexport jdouble jnicall java_compute_comp (jnienv *, jobject, jdoublearray);} ... can be seen that the JNI function name is divided into three parts: The first is the Java keyword for Java virtual machine identification; then the caller class name (full-defined class name, where the name separator is used instead of the name separator); the final is the corresponding method name, and each segment is divided. The parameters of the JNI function are also consisting of three parts: first is JNIENV *, a pointer to the JNI running environment; the second parameter is static or non-static and the second method of non-static local approach The parameter is a reference to the object, and the second parameter of the static local method is a reference to its Java class; the remaining parameters correspond to the parameters of the usual Java method, the parameter type needs to be mapping according to a certain rule. 4. Remote the implementation code of the corresponding method according to the header file. Due to the limit, the specific implementation portion is not described herein. During the encoding process, you need to pay attention to the length problem of the variable, such as the integer variable length of Java is 32 bits, and the C language is 16 bits, so the variable type mapping table is to prevent problems during the transmission value. 5. Compile the JNI implement code into a dynamic link library using the C / C compiler. This link library needs to be explicitly called in the caller class. In Win32 environments, Visual C or other C / C compiler that can generate DLL files into dynamic link libraries can be implemented. The author uses the compiler of Microsoft.NET Framework. The compilation instruction is as follows, the% java_home% is the author's JDK installation directory variable: CL -I% java_home% / include -i% java_home% / include / win32 -ld jnicomp. C -femathlib. DLL under Sun Soloaris, the corresponding instruction is : Cc -g -i / usr / local / java / include -i / usr / local / java / include / solris jnicomp. C / -o mathlib. SO Note, you need to include the necessary library file path when compiling . After the above treatment, a Java class containing the localization method is basically completed. The application of JNI technology is some of the main Java technology, such as JDBC and RMI, most of which are implemented in JNI. However, using JNI does affect the platform independence of the program, it can only be used in special needs.