JVM 01-jvm and Java architecture

Posted by zoozoo on Wed, 09 Feb 2022 16:41:10 +0100

1 JVM and Java architecture

Java is not the most powerful language, but JVM is the most powerful virtual machine

1.1 JVM is a cross language platform

The JVM needs class files as raw materials, which are compiled and interpreted into machine instructions for execution

A class we have written in the IDE is Java end of the file, but in the execution will be compiled into Class file. When a java file starts to be executed, it will pass through:

.java file -> after Java compiler(Front end compilation) 
		-> .class file -> JVM load class The file is compiled and interpreted into machine instructions for execution

class file generated after Java file compilation:

The JVM has nothing to do with languages. It only needs class files that conform to the JVM specification, not just Java. If a language can also be compiled into class files and conform to the JVM specification, it can also be executed by the JVM, that is, the JVM is a cross language platform:

In a project, if the parallel processing is written in Clojure, the presentation layer is written in JRuby, and the middle layer is Java, each application layer is completed in different programming languages, it is completely feasible to use multiple languages for mixed programming on the JVM, because the class files compiled by these languages will run on the same JVM

1.2 introduction and characteristics of JVM

JVM is a virtual machine, which is a software used to execute a series of virtual computer instructions. Virtual machines can be roughly divided into two categories:

System virtual machine
	as VMware, Visual Box System virtual machine is the simulation of physical computer
	It provides a software platform that can run a complete operating system

Application virtual machine
	The most typical is JVM,Designed to execute a single program
	JVM The instructions executed in are also called bytecode instructions, i.e class Contents of the document
	JVM This software is responsible for compiling and interpreting class The content of the document is CPU Understand and execute machine instructions

JVM: a virtual computer that executes class files. It has an independent running mechanism. Various languages on the JVM platform can be compiled into class files and executed on the JVM. It shares the cross platform nature of the JVM. It has excellent garbage collector, reliable real-time compiler, automatic memory management

The JVM runs on the operating system and has no direct interaction with the hardware. The JVMs on different operating systems are different. Different hardware systems perform the same operation in different machine languages. Therefore, there must be a JVM on each system. The JVM on the system is responsible for compiling and interpreting the class file as the machine instructions that can be recognized by the system

Different JVM s are required on different systems:

The so-called write once, run anywhere of Java program means that the written Java program can run directly on other machines on the same platform without modification. In essence, the JVM installed on all machines on the same platform is the same, and the execution method of the same class file is the same

1.3 overall structure of JVM

At present, the most widely used JVM is HotSpot VM, which adopts the architecture of coexistence of interpreter and instant compiler. With the continuous optimization and update of JVM, the running performance of Java program is almost the same as that of C/C + +

1.4 architecture model of JVM

There are two instruction set architectures, one is stack based instruction set architecture, the other is register based instruction set architecture, and the JVM adopts stack based instruction set architecture

Features of the two architectures:

Stack based instruction set architecture:
1,Simpler design and implementation, suitable for resource constrained systems
2,It avoids the problem of register allocation and uses zero address instruction
3,Most of the instructions in the instruction stream are zero address instructions, and the execution process depends on the stack
	The instruction set is smaller and the compiler is easier to implement
4,No hardware support, better portability and better cross platform

Register based instruction set architecture:
1,typical x86 Binary instruction set, such as traditional PC
2,The instruction set completely depends on hardware and has poor portability
3,Excellent performance and more efficient execution
4,Spend less instructions to complete an operation
5,The instruction set based on register architecture is often
	One address instruction, two address instruction and three address instruction
	The instruction set architecture based on stack is mainly zero address instruction

To perform operations like 2 + 3, the instructions of the two architectures are as follows:

Do a simple test to see the instruction set architecture of the JVM:

The above class only performs a simple assignment operation. After running, you can find its compiled bytecode file in the out Directory:

Open the console in IDEA, switch to the directory where the class file is located through cd, and decompile the bytecode file with javap -v:

Here we focus on the contents of the stack:

D:\JVM\About_JVM01\JVM_01\out\production\JVM_01\com\coisini\jvm\core>javap -v StackStuckTest.class
// Some contents are omitted

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=4, args_size=1
         0: iconst_2 // Define constant 2
         1: istore_1 // Store the constant 2 in the stack where the subscript is 1
         2: iconst_3 // Define constant 3
         3: istore_2 // Store the constant 3 in the stack with the subscript 2
         4: iload_1  // Load constant 2
         5: iload_2  // Load constant 3
         6: iadd     // Add two constants
         7: istore_3 // Store the result in the stack at the subscript 3
         8: return
      LineNumberTable:
        line 12: 0
        line 13: 2
        line 14: 4
        line 16: 8
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       9     0  args   [Ljava/lang/String;
            2       7     1     i   I
            4       5     2     j   I
            8       1     3     k   I
}
SourceFile: "StackStuckTest.java"

The three lines of code in the java file are actually translated into eight lines of instructions, which are operated through the stack

Summary:
Due to the cross platform design of the JVM, Java instructions are designed according to the stack. The CPU architectures of different platforms are different, so they can not be designed based on registers. The advantages are cross platform, small instruction set, many instructions and easy implementation of the compiler. The disadvantage is that the performance is degraded, and more instructions are required to realize the same function

1.5 JVM life cycle

The JVM life cycle is mainly divided into three stages:

1. JVM startup (class loading):
The JVM is started through the bootstrap class loader
Create an initial class to complete
This class is specified by the concrete implementation of the virtual machine

When you want to execute a Java program, you need to start the JVM to load the class, that is, to load all the classes needed to execute the main method (the classes appearing in main and their parent classes...)

2. JVM execution (start a new process):
When executing a java program, you are actually creating a Java virtual machine instance
This Java virtual machine instance is a process responsible for executing Java programs, which is used to translate class files into machine instructions and execute them

You can use jps in the console to view the processes being executed in the current JVM:

3. JVM shutdown (program end or exception):

Several situations of withdrawal
1,JVM The normal execution of the program in is over

2,The program is terminated due to an exception or error during execution

3,Caused by an error in the operating system JVM termination

4,Called System.exit()Method, exit directly
...

1.6 development history of JVM

Sun Classic VM->
	Sun Released Sun Classic VM The world's first commercial JVM
	Only interpreter(Line by line explanation),No, JIT compiler(Find hot code and cache)

HotSpot VM->
	yes JDK The default virtual machine, server, desktop, mobile terminal and embedded have applications
	HotSpot It refers to the hot code detection technology, which finds the code with the most compilation value through the counter
	Trigger immediate compilation or replacement on the stack, and work together through the compiler and interpreter

JRockit->
	Focusing on server-side applications, there is no interpreter to realize it, and all the code depends on JIT Execute after compilation
	It's the fastest in the world JVM,Suitable for finance, military command, telecommunications network
	
J9->
	Market positioning and HotSpot Proximity, server side, desktop application, embedded and other multi-purpose JVM
	Widely used IBM Various Java product

Azul VM->
	Proprietary virtual machines bound to specific hardware platforms and used in combination of software and hardware have limited application scenarios
	stay HotSpot A large number of modifications have been carried out on the basis of each Azul VM example
	Can manage at least dozens CPU And hundreds GB Hardware resources of memory

TaobaoJVM->
	Released by Alibaba, it covers many fields. In order to solve the composite problems of high concurrency, high availability and distribution
	abbreviation AJDK,It's the whole Ali Java The cornerstone of the system, but the hardware is heavily dependent intel of CPU
	Lost compatibility and improved performance

Graal VM->
	stay HotSpot Cross language full stack virtual machine enhanced on
	It can be used as the running platform of "any language" and supports the mixing of different languages API
	The working principle is to convert the intermediate format of the source or source code of these languages through the interpreter
	by Graal VM Recognizable documents are most likely to replace HotSpot VM