One: Introduction
Before learning the Java virtual machine, that is, before the Jvm , I think everyone can learn with questions. In this case, everyone will learn more and more!
1. What is the Java Virtual Machine ( JVM )? 2. What is the Java Virtual Machine used for? 3. What is the architecture of the Java virtual machine? 4. What role does the Java virtual machine play in working? 5. Java virtual machine in the runtime data area?
Two: JVM basic concepts
A Java Virtual Machine (JVM) is an imaginary computer that can run Java code
The Java virtual machine includes a bytecode instruction set, a set of registers, a stack, a garbage collection heap, and a storage method field.
Before learning about Jvm , if you are interested, you can also learn about the heap and stack in Java . You can watch the previous article: The difference between Java heap and stack http://blog.csdn.net/u011546655/article/ details/52170470
Three: JVM
We all know that Java source files, through the compiler, can produce the corresponding .Class files, that is, bytecode files, and the bytecode files pass through the interpreter in the Java virtual machine, that is, all the previous Java virtual machines. The bytecode instruction set .... compiles into machine code on a specific machine
That is, as follows:
1. Java source file --> compiler --> bytecode file
2. Bytecode file -->Jvm--> Machine code
The interpreter for each platform is different, but the virtual machine implemented is the same. This is why Java is cross-platform.
A virtual machine is instantiated when a program starts running a program. When multiple programs are started, multiple virtual machine instances exist. The program exits or closes. The virtual machine instance dies. Data cannot be shared among multiple virtual machine instances.
Four: JVM architecture
The architecture of JVM is as follows:
1. Garbage collector
Garbage collector (also known as gc ): It is responsible for recycling useless objects in memory (as if people on earth know that), that is, these objects do not have any references, it will be regarded as: garbage, and it will be killed.
2. Class loading subsystem
As soon as you hear the name, everyone knows that it must be the system used to operate our compiled .Class files. Its functions are as follows:
vLocate and import binary class files
vVerify the correctness of imported classes
v allocate initialization memory for the class
v help resolve symbolic references
3. Execution Engine ( Execution Engine )
Execute the instructions wrapped in the method of the loaded class, that is, the method
4. Operating area data
As shown in the figure above: virtual machine memory or JVM memory, open up a piece of memory in the entire computer memory to store the objects, variables, etc. that the JVM needs to use. The data in the runtime area is divided into many cells, namely: method area, virtual machine stack, and local method stack. , heap, program counter
Five: JVM runtime data area
1. Program Counter
The signal indicator that the current thread executes the bytecode. The thread is private. Its life cycle and the basic functions of the thread, such as branching, looping, jumping, exception handling, and thread recovery, all need to rely on this counter to complete.
2. Virtual machine stack
The Java virtual machine stack describes the memory model for the execution of Java methods (different from native methods): when each method is executed, a stack frame ( Stack Frame ) is created at the same time to store the local variable table, operation stack, and action Links, method exits, etc.
The thread is private, the life cycle is the same as the thread, and there are independent counters, which do not affect each other.
The process of each method being called until the execution is completed corresponds to the process of a stack frame from being pushed to the stack in the virtual machine stack.
3. Native method stack
Similar to the virtual machine method stack, but the local method stack serves the Native method service used by the virtual machine
4. Local method area:
Just execute the Native method.
If there is insufficient memory in this area, StackOverflowError and OutOfMemoryError exceptions will also be thrown.
Six: heap
The heap area is the largest in the JVM , and the objects and data of the application are stored in this area. This area is also shared by threads. It is also the main recycling area of GC .
In fact, this is what everyone hears a lot: Java ( gc ) garbage collector
Due to space reasons, I will focus on explaining in the next article: Principle analysis of Java gc collector