-
- In 2011,
JDK7
In 1.7 U4, the new garbage collector is enabledG1
(But not by default).
- In 2011,
-
- In 2017, it was released
JDK9
.G1
The defaultGC
To replace theCMS
. (Generally used by companiesjdk8
“, will be specified by parameterGC
forG1
)
- In 2017, it was released
-
- In 2018, it was released
JDK11
Brought about revolutionZGC
, the performance is strong.
- In 2018, it was released
Vm Introduction
Virtual machines, virtual computers, can execute a series of virtual computer instructions, can be roughly divided into system virtual machines and program virtual machines. When they run, they are limited by the resources provided by the virtual machine.
- System virtual machine: emulates the system, for example
Visual Box
.VMware
. - Program virtual machine: designed to execute a single computer program, for example
Java
The virtual machine.
The JAVA virtual machine
A Java virtual machine is a virtual machine that executes bytecodes, but bytecodes are not necessarily compiled from the Java language. But as long as you use the language of the virtual machine rules, you can enjoy cross-platform, garbage collection, and a reliable just-in-time compiler. There is no direct interaction between the JVM and the hardware.
- Compile once, run everywhere.
- Automatic memory management
- Automatic garbage collection
The following is a description of the Java concept map in the AVA platform documentation. You can see that the Javac command in the JDK, which compiles.java files into.class files, is the front-end compiler, which compiles source files into bytecode. This compiler is not included in the JRE, which also shows that the JRE does not include the compilation environment.
Both the JRE and JDK include JVM virtual machines. The JRE is the runtime environment, while the JDK contains the development environment.
Structure of the Java family in JDK7:Docs.oracle.com/javase/7/do…
Structure of the Java family in JDK7:Docs.oracle.com/javase/8/do…
The JVM structures
The diagram above consists of three main parts: the classloader, the runtime data area, and the execution engine.
Class loaders, which load Class files (bytecode files that have been compiled by the front-end compiler) into the runtime data area to generate Class objects, design the loading, linking, initialization, and so on.
The runtime areas are divided into:
- Thread private (one per thread) :
- Program counter:
Program Count Register
, thread private, no garbage collection - Virtual machine stack:
VM Stack
, thread private, no garbage collection - Local method stack:
Native Method Stack
, thread private, no garbage collection
- Program counter:
- Thread sharing:
- Methods area:
Method Area
In order toHotSpot
For example,JDK1.8
Post meta space replaces method area with garbage collection. - Pile:
Heap
The most important place for recycling.
- Methods area:
The execution engine mainly consists of an interpreter and a just-in-time compiler (the hot code is compiled ahead of time, which is the back-end compiler), and a garbage collector. Bytecode files are not directly machine-readable, so an execution engine is needed to do the conversion.
Java code executes the process
In the process of Java code becoming a bytecode file, it actually contains a series of operations such as lexical analysis, syntax analysis, syntax tree and semantic analysis.
In the execution engine, there is a JIT compiler, which is where the second compilation takes place, which compiles hot code into machine instructions, which are cached (in the method area) by method dimension, also known as CodeCache.
JVM architecture Model
The Java compiler is primarily a stack based instruction set architecture. I think the main reason is portability. The JVM needs to be cross-platform. There are two main types of instruction set architectures:
- Stack-based instruction set architecture: a method is equivalent to an operation on the stack, and execution is equivalent to an operation off the stack.
- Register-based instruction set architecture
Characteristics of stack – based instruction – set architecture
Main features:
- Simple design and implementation, suitable for resource-constrained systems, such as set-top boxes, small toys.
- Avoid register allocation problem: use zero address instruction mode allocation.
- Most of the instruction stream is zero-address instruction, the execution process depends on the operation stack, the instruction set is smaller (zero-address), the compiler is easy to implement.
- No need for hardware support, portability, easy to implement cross-platform.
Register-based architecture features
- A typical application is the binary instruction set for x86
- It depends on hardware and has poor portability
- Good performance and high execution efficiency
- Fewer instructions to perform an operation
- In most cases, the register architecture is dominated by one, two, and three address instructions, whereas the stack based instruction set is dominated by zero address instructions.
What is zero address instruction, an address instruction, two address instruction? Zero-address instructions have only opcodes and no operands. This instruction has two cases: one is that operands are not required, and the other is that the operands are default (implicit). By default, the operands are in the register, and the instruction can access the register directly.
-
Three-address instruction: in the general address domain, A1 and A2 determine the addresses of the first and second operands respectively, and A3 determines the result address. The address of the next instruction is usually given in order by the program counter.
-
Two-address instruction: in the address field A1 determines the address of the first operand, A2 determines both the address of the second operand and the address of the result.
-
Single-address instruction: Address field A determines the address of the first operand. Fixed using a register to hold the second operand and the result of the operation. Their addresses are thus implicit in the instruction.
-
Zero-address instruction: In stack-based computers, the operands are generally stored in the two cells at the top of the push stack, and the results are placed at the top of the stack. Addresses are hidden, so most instructions have only opcodes but no address fields.
Stack data structure, generally only on and off the stack, so the operation place is only the top of the stack, so the location is determined, do not need an address.
Example Perform 2+3 if it is a stack based calculation flow:
Iconst_2 // constant 2 pushes istore_1 iconst_3 // Constant 3 pushes istore_2 ILOAD_1 iload_2 iadd // Constant 2,3 pushes istore_0 // result 5 pushes istore_0Copy the code
Register-based calculation process:
Mov eax,2 // add eax,3 // add eax,3 // add eaxCopy the code
As you can see from the example above, the stack register-based instructions are smaller, but there are fewer register-based instructions.
We can take a look at this with a simple program:
public class StackStructTest {
public static void main(String[] args) {
int i = 2 + 3; }}Copy the code
After compiling, switch to the class directory and decompile using the command:
java -v StackStructTest.class
Copy the code
If you look at the bytecode module, you can see iconst_5 in front of it. In fact, 5 is the result of 2+3, that is, during compilation, 2+3 will be directly changed into 5, not computed at runtime, because 2 and 3 are constant.
This phenomenon is calledConstant folding at compile time.
But what if we made the code look like this?
int i = 2;
int j = 3;
int k = i + j;
Copy the code
The decompiled instruction:
Const means constant, store is storeage register.
Stack =2, locals=4, args_size=1 0: iconst_2 // 2 is a constant 1: istore_1 // 2 loads onto operand stack 1 2: iconst_3 // 3 is a output 3: Istore_2 // 3 to stack 2 4: iloAD_1 // to stack 1, load in 5: iload_2 // to stack 2, load in 6: iadd // add the two 7: Istore_3 // The result is stored in the operand stack with index 3 8: returnCopy the code
The stack architecture JVM, in other words, requires eight instructions to do the above variable addition.
Summary of stack architecture
Due to the cross-platform characteristics, Java instructions are designed based on stacks. Because different CPU architectures are different, the advantages are cross-platform, small instruction set, and easy implementation by the compiler. The downside is that performance degrades and more instructions are required to perform the same function.
[Author profile] : Qin Huai, public number [Qin Huai Grocery store] author, the road of technology is not at that time, mountain high water long, even slow, chi and not stop. Personal Writing Direction: Java source code analysis, JDBC, Mybatis, Spring, Redis, distributed, sword Offer, LeetCode, etc., carefully write each article, do not like the title party, do not like the flowery, mostly write a series of articles, can not guarantee that I write are completely correct, But I guarantee that what I write is done through practice or research. We hope to correct any omissions or mistakes.
What did I write about 2020?
Open Source Programming Notes
Weekdays time is precious, I can only use the evening and weekend time to learn writing, pay attention to me, let’s grow together ~