JVM Optimization-(1) Overview

Why learn JVM optimization

  • interview needs

The questions that must be asked in the interview of a big factory, the interview does not ask about jvm, concurrency, and distribution. Learning JVM is not just about interviews, but about a deeper understanding of the Java language and laying the foundation for future troubleshooting of online problems.

  • In-depth understanding of the Java language

For friends who have been working for three years, many times when you want to solve a problem, you must go deep into the bytecode level to analyze it, and you can get accurate conclusions, and the bytecode is a part of the virtual machine. Example: 1. Our commonly used Boolean Boolean, we all know that it has two values, true and false. But you know that at runtime, the Java virtual machine does not have a Boolean type. The Boolean type is represented by integer 1 and 0 in the virtual machine.

  • Learning virtual machines is to better solve online troubleshooting problems in the future

We know that when we deploy a Java application on an online machine, there will definitely be problems from time to time. Apart from the problems of the network and the system itself, many times there is a problem with the Java application, which means that there is a problem with the memory of the Java virtual machine. Either the memory is overflowing, or the frequent GC leads to slow response and so on. So how to solve these problems?

Chapter 1 JVM Review

What is JVM

  • What is JVM

JVM is the abbreviation of Java Virtual Machine (Java Virtual Machine). JVM is a specification for computing devices. It is a fictitious computer that is realized by simulating various computer functions on an actual computer.

  • mainstream virtual machine
virtual machine name introduce
HotSpot Both Oracle/Sun JDK and OpenJDK use the same core of the HotSPot VM
J9 J9 is a highly modular JVM developed by IBM
JRockit Both JRockit and HotSpot belong to Oracle. So far, Oracle has been promoting the fusion and complementarity of HotSpot and JRockit, two virtual machines with their own advantages.
Zing A high-performance and low-latency JVM improved by Azul Systems based on HostPot
Dalvik Although Dalvik on Android is not called JVM, it is an out-and-out JVM in its bones

2 JVM and operating system

  • Why add a JVM between the program and the operating system

Java is a language with a particularly high degree of abstraction, providing a series of features such as automatic memory management. It is impossible to implement these features directly on the operating system, so the JVM needs to be converted.

insert image description here

As can be seen from the figure, with the abstraction layer of JVM, Java can achieve cross-platform. The JVM only needs to ensure that the .class files can be executed correctly, and then it can run on platforms such as Linux, Windows, and MacOS. The significance of Java cross-platform is to compile once and run everywhere, and the JVM is indispensable for being able to do this. For example, if we download the jar package of the same version in the Maven warehouse, it can run everywhere without recompiling on each platform. Some of the current JVM extension languages, such as Clojure, JRuby, Groovy, etc., are compiled to .class files at the end. Java language maintainers only need to control the JVM parser to run these extension languages ​​seamlessly. on top of the JVM.

  • Relationship between application, JVM, operating system

insert image description here

We summarize the relationship between the JVM and the operating system in one sentence: the JVM inherits the development language from the top, and connects to the operating system from the bottom, and its intermediate interface is bytecode.

3 Relationship between JVM, JRE, and JDK

insert image description here

The JVM is at the heart of how Java programs can run. But it should be noted that the JVM can't do anything by itself, you need to provide it with production raw materials (.class files). Only the JVM cannot compile once and run everywhere. It needs a basic class library, such as how to operate files, how to connect to the network, and so on. The Java system is very generous, and will pass all the class libraries required for the JVM to run to it at one time. The JVM standard plus a large number of basic class libraries implemented constitute the Java runtime environment, which is what we often call JRE (Java Runtime Environment). For the JDK, it is even larger. In addition to JRE, JDK also provides some very useful gadgets, such as javac, java, jar, etc. It is the core of Java development, allowing laymen to practice swords! We can also look at the full spelling of JDK, Java Development Kit. I am very afraid of the word kit (equipment), it is like a bottomless pit, indicating that you will never end to study it. The relationship between JVM, JRE, and JDK can be represented by an inclusion relationship.
insert image description here

4 Relationship between Java Virtual Machine Specification and Java Language Specification

insert image description here

The left half is the Java virtual machine specification, which actually provides an operating environment for inputting and executing bytecodes. The right half is the Java grammar specification we often say, such as switch, for, generics, lambda and other related programs, which will eventually be compiled into bytecode. The bridge connecting the left and right parts is still Java bytecode. If the specification of the .class file is constant, these two parts can be optimized independently. But Java also occasionally expands the format of the .class file, adding some bytecode instructions to support more features. We can regard the Java virtual machine as an abstract computer, which has its own instruction set and various runtime memory areas. Students who have studied "Computer Composition Structure" will see a lot of similarities later in the course. sex.

Finally, let's take a brief look at the execution process of a Java program and how it runs.
insert image description here
The Java program here is in text format. For example, the following section of HelloWorld.java follows the Java language specification. Among them, we called modules such as System.out, which is the class library provided in JRE.

public class HelloWorld {
    
    
	public static void main(String[] args) {
    
    
		System.out.println("Hello World");    
	}
}

After compiling with the JDK tool javac, the bytecode of HelloWorld will be generated. We have been saying that Java bytecode is a bridge between the JVM and Java programs. Let's use javap to see what the bytecode looks like.

0 getstatic #2 <java/lang/System.out> // getstatic 获取静态字段的值

3 ldc #3<HelloWorld> // ldc 常量池中的常量值入栈

5 invokevirtual #4 <java/io/PrintStream.println> // invokevirtual   运行时方法绑定调用方法

8 return //void 函数返回

The Java virtual machine uses a stack-based architecture, and its instructions consist of opcodes and operands. These bytecode instructions are called opcodes. Among them, getstatic, ldc, invokevirtual, return, etc. are opcodes, which are relatively easy to understand. The JVM completes program execution by parsing these opcodes and operands. When we use the Java command to run a .class file, it is actually equivalent to starting a JVM process.
The JVM then translates these bytecodes, which can be executed in two ways. The common one is interpretation and execution, which translates opcode + operands into machine code; another execution method is JIT, which is what we often call just-in-time compilation, which compiles bytecode into machine code under certain conditions before executing it .

Guess you like

Origin blog.csdn.net/qq_31686241/article/details/128050495