Run the process
We all know that Java has been promoting the slogan is: Compile, run everywhere.
After a compilation of Java program, the Java code is compiled into a byte code is a class file, and then on different operating systems rely on different Java virtual machines for interpretation, and finally converted to different platform machine code, and finally implemented. So we can deduce that if you want to run on a Mac system, you just need to install a Mac Java Virtual machine. Now that we've learned the basics, we're trying to do a deeper study of what it's like to do with a normal Java program. For example, we wrote a piece of code like this:
public class HelloWorld {public
static void Main (string[] args) {
System.out.print (' Hello World ');
}
This program from compile to run, and finally print out "Hello World" what steps have passed.
The Java code generates a bytecode file (class file) after compiling, and is executed by: Java HelloWorld, At this time Java based on the system version to find Jvm.cfg, you can search your computer on the jvm.cfg file, it will be based on your system version placed in a different location, such as my file is in: C:\Program files\java\jdk1.8.0_101 \jre\lib\amd64\jvm.cfg, open and look:
This is the file on my computer, where-server known means that the JVM named server is available. If you search your computer for Jvm.dll, you will find that it must be in one of your server directories, such as mine: C:\Program Files\java\jdk1.8.0_101\jre\bin\server\jvm.dll. In short, finding the corresponding Jvm.dll,jvm.dll through the Jvm.cfg file is the main implementation of the Java Virtual machine. The JVM is then initialized and the JNI interface is obtained, what is the JNI interface, the Java local interface, you think? Java is compiled into a class file, how the JVM finds it from the hard disk and loads it into the JVM, through the JNI interface (which is also used in Java and the operating system , hardware interaction), locate the class file and load it into the JVM, then locate the main method, and finally execute. The basic structure of the JVM
So how does the JVM internally execute a class file, which is the inner detail of the 6th step of the last step in the diagram above? To understand this issue, we first need to look at the internal structure of the JVM:
From this structure it is not difficult to see that the class file is loaded by the JVM, after the JVM's memory space provisioning, and ultimately by the execution engine to complete the execution of the class file. Of course, this process also has other role modules to assist, these modules to work together to make a successful Java program to run, the following details of these templates, they are also learning the most important part of the JVM later. Memory Space:
The JVM memory space contains: Method area, Java heap, Java stack, local method stack.
The method area is the area shared by each thread, storing class information, constants, and static variables.
The Java heap is also a thread-shared area where the instances of our classes are placed in this area, and you can imagine that one of your systems produces many instances, so the Java heap has the largest space. If the Java heap does not have enough space, the program throws a OutOfMemoryError exception.
The Java stack is a private area of each thread, and its lifecycle is the same as the thread, a thread corresponds to a Java stack, each execution of a method will be pushed into the stack of an element, this element is called "stack frame", and the stack frame includes the method of local variables, to store the middle State value of the Operation Stack, which has a lot of details, We'll talk about it later. If the Java stack space is not enough, the program will throw Stackoverflowerror exception, think of what circumstances will easily produce this error, yes, recursion, if the depth is very deep, you will execute a large number of methods, the more Java stack occupy space.
The local method stack role is similar to the Java stack, except that it is used to indicate that local methods are executed locally, and the local method stack stores methods that invoke the local method interface, and ultimately invoke the local method library to interact with the operating system and hardware.
PC registers, and here our class has been loaded, instance objects, methods, static variables have gone to their own changes to the place, then the problem, how to execute the program, which method to execute first, which method after the implementation of the order of these instructions is the PC register in the tube, it is the role of the control program instructions order of execution.
The execution engine, of course, is to execute the program instruction sequentially according to the order of the PC registers.
Reprint Link: HTTP://WWW.JIANSHU.COM/P/EC971F4C6C04