JVM principle and optimization, and JVM principle Optimization
The jvmjob principle and special point are that the operating system is installed with jvmthrough jdk's java.exe, and the following four steps are used to complete the JVM environment.
1. Create a JVM loading environment and Configuration
2. Load JVM. dll
3. initialize JVM. dll and mount it to the JNIENV (JNI call interface) instance.
4. Call the JNIEnv instance to load and process the class.
A jvm concept is often mentioned when we run and debug Java programs. JVM is the environment where Java programs run, but it is an application and a process of the operating system at the same time. Therefore, it also has its own running life cycle, it also has its own code and data space.
First, let's talk about the jdk In the JVM working principle. Whether you are a beginner or a master, a j2ee programmer or a j2se programmer, jdk is always doing something for us. before learning about Java, the masters will provide us with jdk. what role does it play in the entire Java System? I'm amazed by the design talents of masters sun, who can make such a complete system so perfectly structured. in this system, jdk acts as a production and processing center to generate all data outputs. It is the execution center of all commands and strategies. it provides a complete Java solution and can develop all the applications and system programs currently supported by Java. here is a question. You may ask, why are there other such things as j2-j2ee? These two things are designed to simplify the development and construction processes in their respective fields. in addition to JVM, jdk also includes core APIs, integrated APIs, user tools, development technologies, development tools, and APIs.
Well, I have talked a lot about theme-related things. JVM is at the bottom layer of the entire jdk and is responsible for operating system interaction. It is used to shield the operating system environment and provide a complete Java running environment. Therefore, it is also a virtual computer. the operating system is installed with jvmthrough java.exe in JDK. The following four steps are used to complete the JVM environment.
1. Create a JVM loading environment and Configuration
2. Load JVM. dll
3. initialize JVM. dll and mount it to the JNIENV (JNI call interface) instance.
4. Call the JNIEnv instance to load and process the class.
1. JVM is loaded into the environment. The JVM provides dynamic connection files of the operating system.Since it is a file, it is a mount path problem. How does Java find this path? When you call Java test', the operating system will be in your java.exeprogram under path. java.exe uses the following process to determine the JVM path and related parameter configurations. The following is an analysis of the implementation based on Windows.
First, find the jre path. Java uses GetApplicationHome apito upload the absolute path of Java. c: \ j2sdk1.4.2 _ 09 \ bin \ Java.exe. Then it intercepts the absolute path c: \ j2sdk1.4.2 _ 09 \, judge c: \ j2sdk1.4.2 _ 09 \ bin \ Java. whether the dll file exists. If yes, use c: \ j2sdk1.4.2 _ 09 \ as the jre path. If no, use c: \ j2sdk1.4.2 _ 09 \ jre \ bin \ Java. whether the dll exists. If the c: \ j2sdk1.4.2 _ 09 \ jre exists, use it as the jre path. if you do not call GetPublicJREHome To Check HKEY_LOCAL_MACHINE \ Software \ JavaSoft \ Java Runtime Environment \ "Current JRE version number" \ JavaHome, the path is jre path.
Then load the JVM. cfg file JRE path + \ lib + \ ARCH (CPU architecture) + \ JVM. the cfcompetition (CPU architecture) is determined by the GetArch function in Java_md.c. In this function, there are only two windows platforms: 'ia64' in WIN64 and 'i386' in other cases '. Take my example: C: \ j2sdk1.4.2 _ 09 \ jre \ lib \ i386 \ JVM. cfg. The main content is as follows:
- -Client KNOWN
- -Server KNOWN
- -Hotspot ALIASED_TO-client
- -Classic WARN
- -Native ERROR
- -Green ERROR
In our jdk directory, jre \ bin \ server and jre \ bin \ client both have JVM. the dll file exists, while Java uses JVM. cfg configuration file to manage these different versions of JVM. dll. through the file, we can define the JVM supported by jdk. The previous part (client) is the JVM name, followed by the parameter. KNOWN indicates that the JVM exists, and ALIASED_TO indicates that an alias is given to other JVMs, WARN indicates finding a JVM replacement when there is no such exception, and ERROR indicates that no exception is thrown. when running Java xxx, java.exe checks the current JVM type through CheckJVMType. Java can specify the specific JVM type through two parameters, one according to JVM. specify the JVM name in the cfg file. The second method is to directly specify the name, the methods they run are "Java-J", "Java-XXaltJVM =", and "Java-J-XXaltJVM = ". For the first parameter transfer method, the CheckJVMType function takes the JVM name after the '-J' parameter, then, find from the known JVM configuration parameters. If the same name is found, remove the '-' before the JVM name to directly return the value. The second method, the JVM type name after "-XXaltJVM =" or "-J-XXaltJVM =" is directly returned. If no one of the above two methods is specified when running Java, checkJVMType takes the JVM name in the first configuration in the configuration file, and removes '-' before the name to return this value. The Return Value of the CheckJVMType function is combined with the jre path in the following function to form the absolute path of JVM. dll. If this parameter is not specified, the first defined JVM in JVM. cfg will be used. You can test it on the console through set _ Java_LAUNCHER_DEBUG = 1.
Finally, obtain the JVM. dll path, JRE path + \ bin + \ JVM type string + \ JVM. dll is the JVM file path, but if you use the path specified by the-XXaltJVM = parameter when calling the Java program, you can directly use path + \ JVM. dll file as JVM. dll file path.
Ii. Load JVM. dll
The JVM path has been found through the first step. Java uses LoadJavaVM to mount the JVM. dll file. The installation is easy to call the Windows API function:
LoadLibrary loads the JVM. dll dynamic connection library. Then, the export functions JNI_CreateJavaVM and jni_getdefajavavminitargs in JVM. dll are mounted to the CreateJavaVM and getdefajavjavavminitargs function pointer variables of the InvocationFunctions variable. The installation of JVM. dll is completed.
3. initialize JVM and obtain the local call interface,In this way, you can call JVM functions in Java. Call InvocationFunctions-> CreateJavaVM, that is, the JNI_CreateJavaVM method in JVM to obtain the JNIEnv structure instance.
4. Run the Java program.
Java programs can be either a jar package or a class program. when running jar and Java-jar xxx.jarruntime, java.exe calls the GetMainClassName function. This function obtains the JNIEnv instance and then calls the Java class Java. util. jar. the method getManifest () in JarFileJNIEnv and get the value of getAttributes ("Main-Class") from the returned Manifest object is the file in the jar package: META-INF/MANIFEST. the Main Class name specified by MF is used as the Main Class for running. The main function then calls the LoadClass method in Java. c to load the main class (using FindClass of the JNIEnv instance ). The main function directly calls the LoadClass method in Java. c to load the class. If the class method is executed. The main function directly calls the LoadClass method in Java. c to load the class.
Then the main function calls the GetStaticMethodID method of the JNIEnv instance to find the loaded class main class.
"Public static void main (String [] args)" method, determine whether the method is a public method, and then call the JNIEnv instance's
The CallStaticVoidMethod method calls the main method of the Java class.
Below