1.jvm and java architecture

jvm introduction

JVM: a cross-language platform

Java is one of the most widely used software development platforms currently. As Java and the Java community continue to grow, Java is no longer simply a computer language, it is a platform, a culture, and a community.

● As a platform, the Java virtual machine plays a decisive role
. ○ Groovy, Scala, JRuby, Kotlin, etc. are all part of the Java platform
. ● As a kind of culture, Java has almost become synonymous with "open source".
○ Third-party open source software and frameworks. Such as Tomcat, Struts, MyBatis, Spring, etc.
○ Even JDK and JVM themselves have many open source implementations, such as openJDK and Harmony.
● As a community, Java has the largest number of technical advocates and open source community support in the world, with countless forums and information. From desktop application software, embedded development to enterprise-level applications, backend servers, and middleware, Java can be seen everywhere. The complexity of its application forms and the number of participants are staggering.
Insert image description here
Each language needs to be converted into bytecode files, and the final converted bytecode files can be run and processed by the Java virtual machine. ● With the official release of Java7, the
Insert image description here
designers of the Java virtual machine have basically adopted the JSR-292 specification. Enables running programs written in non-Java languages ​​on the Java virtual machine platform .
● The Java virtual machine doesn't care at all what programming language the program running inside it is written in. It only cares about bytecode files. In other words, the Java virtual machine is language-independent and will not simply be "bound" to the Java language for life, as long as the compilation results of other programming languages ​​meet and include the internal instruction set, symbol table and other auxiliary information of the Java virtual machine. , it is a valid bytecode file that can be recognized and loaded by the virtual machine.

bytecode

● The java bytecode we usually talk about refers to the bytecode compiled using the java language. To be precise, any bytecode format that can be executed on the jvm platform is the same. So it should be collectively called: jvm bytecode.
● Different compilers can compile the same bytecode files, and the bytecode files can also be run on different JVMs.
● The Java virtual machine has no necessary connection with the Java language. It is only associated with a specific binary file format - the Class file format. The Class file contains the Java virtual machine instruction set (or bytecode, Bytecodes) and symbols. table, and some other auxiliary information.

Multi-language hybrid programming

Multi-language hybrid programming on the Java platform is becoming mainstream. Solving problems in specific fields through languages ​​in specific fields is a direction for current software development to cope with increasingly complex project needs .
● Just imagine that in a project, parallel processing is written in Clojure language, the presentation layer uses JRuby/Rails, and the middle layer is Java. Each application layer will be completed using a different programming language, and the interface is different for each Developers of the layers are transparent, and there is no difficulty in interacting between various languages. It is as convenient as using the native API of your own language, because they all ultimately run on a virtual machine .
● For these languages ​​running on the Java virtual machine and other than Java, system-level and underlying support is rapidly increasing, with a series of projects and functional improvements centered on JSR-292 (such as the Da Vinci Machine project, Nashorn engine, InvokeDynamic instruction, java.lang.invoke package, etc.), promoting the development of Java virtual machine from a Java language virtual machine to a multi-language virtual machine .

How to really understand JVM?

The Java virtual machine is very complex, and the best way to truly understand how it works is to write one yourself!

Is it difficult to write a Java virtual machine by yourself?

Are things in the world difficult or easy?

If you do it, the difficult things will become easy; if you don’t do it, the easy things will also become difficult.
Insert image description here

Major events in the development of Java

● In 1990, at Sun Computer Company, the Green Team, led by Patrick Naughton, Mike Sheridan and James Gosling, developed a new programming language named oak, later named Java ● In 1995, Sun officially released Java and
HotJava product, Java makes its first public appearance.
● On January 23, 1996, Sun Microsystems released JDK 1.0.
● In 1998, JDK1.2 version was released. At the same time, sun released JSP/Servlet and EJB specifications, and divided Java into J2EE, J2SE and J2ME. This shows that Java is beginning to advance into the three major areas of enterprise, desktop applications and mobile device applications.
● In 2000, JDK1.3 was released, and Java HotSpot Virtual Machine was officially released and became the default virtual machine for Java.
● In 2002, JDK1.4 was released, and the ancient Classic virtual machine retired from the stage of history.
● At the end of 2003, Scala of the Java platform was officially released, and Groovy also joined the Java camp in the same year.
In 2004, JDK1.5 was released. At the same time, JDK1.5 was renamed JavaSE5.0.
● In 2006, JDK6 was released. In the same year, Java was open sourced and OpenJDK was established. Naturally, the Hotspot virtual machine has also become the default virtual machine in openJDK .
● In 2007, the Java platform welcomed a new partner, Clojure.
● In 2008, Oracle acquired BEA and obtained the JRockit virtual machine.
● In 2009, Twitter announced that it would migrate most of its backend programs from Ruby to Scala, which was another large-scale application of the Java platform.
● In 2010, Oracle acquired Sun and obtained the Java trademark and the most valuable HotSpot virtual machine. At this time, Oracle has the two virtual machines with the highest market occupancy, HotSpot and JRockit, and plans to integrate them in the future: HotRockit
● In 2011, JDK7 was released. In JDK1.7u4, the new garbage collector G1 is officially enabled .
● In 2017, JDK9 was released. Set G1 as the default Gc, replacing CMS
● In the same year, IBM's J9 was open sourced, forming the current Open J9 community
● In 2018, Android's Java infringement case was judged, and Google compensated Oracle for a total of US$8.8 billion
● In the same year, Oracle announced that JavaEE has become history Nouns JDBC, JMS, and Servlet were donated to the Eclipse Foundation
● In the same year, JDK11 was released, the LTS version of JDK, the revolutionary ZGC was released , and the JDK license was adjusted
● In 2019, JDK12 was released, joining the shenandoah GC led by RedHat.

Before JDK11, there will be some closed-source functions in OracleJDK that are not found in OpenJDK. But in JDK11, we can think that the OpenJDK and OracleJDK codes are essentially completely consistent.

However, the mainstream JDK 8 was announced to stop updating after January 2019. In addition, JDK 11 and later versions no longer provide free long-term support (LTS), and JDK 15 and JDK 16 are not long-term support versions. The latest JDK 15 is only supported for 6 months, until March 2021. , so never use non-long-term support versions such as JDK 15 in production.

Virtual machine and java virtual machine

virtual machine

The so-called virtual machine (Virtual Machine) is a virtual computer. It is a piece of software used to execute a series of virtual computer instructions. Generally speaking, virtual machines can be divided into system virtual machines and program virtual machines.

● The famous Visual Box and Mware are system virtual machines. They are completely simulations of physical computers and provide a software platform that can run a complete operating system.
● The typical representative of a program virtual machine is the Java virtual machine, which is specially designed to execute a single computer program. The instructions executed in the Java virtual machine are called Java bytecode instructions.

Whether it is a system virtual machine or a program virtual machine, the software running on it is limited to the resources provided by the virtual machine.

Java virtual machine

● The Java virtual machine is a virtual computer that executes Java bytecode. It has an independent operating mechanism, and the Java bytecode it runs may not be compiled by the Java language.
● Various languages ​​​​of the JVM platform can share the cross-platform nature brought by the Java virtual machine, the excellent garbage collector, and the reliable just-in-time compiler.
● The core of Java technology is the Java Virtual Machine (JVM, Java Virtual Machine), because all Java programs run inside the Java Virtual Machine.

effect

● The Java virtual machine is the running environment of binary bytecode. It is responsible for loading the bytecode into its interior and interpreting/compiling it into machine instructions for execution on the corresponding platform. Each Java instruction has a detailed definition in the Java virtual machine specification, such as how to obtain the operands, how to process the operands, and where to store the processing results.

Features

● Compile once, run anywhere
● Automatic memory management
● Automatic garbage collection function

The location of the JVM.
Insert image description here
The JVM runs on the operating system and has no direct interaction with the hardware.
Insert image description here

The overall structure of JVM

Insert image description here
● HotSpot VM is one of the representative high-performance virtual machines currently on the market.
● It adopts an architecture in which an interpreter and a just-in-time compiler coexist.
● Today, the running performance of Java programs has been completely transformed, and it has reached a point where it can compete with C/C++ programs.

Notes
1. It used to be called java stack, but now it’s called virtual machine stack
2. Multiple threads share the heap and method area

Java code execution process

Insert image description here

Note:

During the compilation process of the Java compiler, if any node fails to execute, the compilation will fail.

Although the internal implementation details of the Java virtual machines of each platform are different, the bytecode content they jointly execute is the same.

The main task of the JVM is to load bytecode into its interior and interpret/compile it into machine instructions (ie: assembly language) on the corresponding platform for execution.

The Java virtual machine uses a class loader to load class files.
After the class loading is completed, bytecode verification will be performed. After the bytecode verification passes, the JVM interpreter will translate the bytecode into machine code (ie: assembly language) and hand it over to the operating system for execution.

But not all code is interpreted and executed, and the JVM has optimized this. For example, take the Hotspot virtual machine, which itself provides JIT (Just In Time)

JVM architectural model,

The instruction stream input by the Java compiler is basically a stack-based instruction set architecture, and the other instruction set architecture is a register-based instruction set architecture.

Specifically: the differences between these two architectures:

Features based on stack architecture

● Simpler design and implementation, suitable for resource-constrained systems
● Avoids the problem of register allocation: uses zero-address instructions to allocate
● Most of the instructions in the instruction stream are zero-address instructions, and their execution process depends on the operation stack. The instruction set is smaller, and the compiler is easy to implement.
● No hardware support is required, and the portability is better, and cross-platform implementation is better.

Features of register-based architecture

● Typical applications are x86 binary instruction sets: such as traditional PCs and Android's Davlik virtual machine
● Instruction set architecture relies entirely on hardware and has poor portability
Excellent performance and more efficient execution
Spends fewer instructions to complete an item Operation
● In most cases, the instruction set based on the register architecture is often dominated by one-address instructions, two-address instructions, and three-address instructions, while the instruction set based on the stack-based architecture is dominated by zero-address instructions.

Example 1

The same logical operation of 2+3 is performed, and the instructions are as follows:

Stack-based computing process (taking Java virtual machine as an example)

iconst_2 //常量2入栈
istore_1
iconst_3 // 常量3入栈
istore_2
iload_1
iload_2
iadd //常量2/3出栈,执行相加
istore_0 // 结果5入栈

The register-based calculation process

mov eax,2 //将eax寄存器的值设为1
add eax,3 //使eax寄存器的值加3

Example 2

public int calc(){
    
    
    int a=100;
    int b=200;
    int c=300;
    return (a + b) * c;
}
> javap -c Test.class
...
public int calc();
    Code:
    Stack=2,Locals=4,Args_size=1
       0: bipush        100
       2: istore_1
       3: sipush        200
       6: istore_2
       7: sipush        300
      10: istore_3
      11: iload_1
      12: iload_2
      13: iadd
      14: iload_3
      15: imul
      16: ireturn
}

Summarize

Due to the cross-platform design, Java instructions are designed based on the stack. Different platforms have different CPU architectures, so they cannot be designed to be register-based. The advantages are that it is cross-platform, the instruction set is small, and the compiler is easy to implement. The disadvantage is that performance is reduced and more instructions are needed to achieve the same function.

Today, although embedded platforms are no longer the mainstream running platform for Java programs (to be precise, the host environment of HotSpotVM is no longer limited to embedded platforms), then why not change the architecture to a register-based architecture?
Stack:
cross-platform, small instruction set, many instructions; execution performance is worse than registers

JVM life cycle

Starting the virtual machine

The startup of the Java virtual machine is completed by creating an initial class (initial class) through the bootstrap class loader. This class is specified by the specific implementation of the virtual machine.

Virtual machine execution

● A running Java virtual machine has a clear task: execute Java programs.
● It runs when the program starts executing and stops when the program ends.
When executing a so-called Java program, what is really being executed is a process called the Java virtual machine.

Exit of virtual machine

There are several situations as follows:

● The program ends normally.
● The program encounters an exception or error during execution and terminates abnormally.
● The Java virtual machine process terminates due to an operating system error.
● A thread calls the exit method of the Runtime class or the system class, or the exit method of the Runtime class. halt method, and the Java security manager also allows this exit or halt operation.
● In addition, the JNI (Java Native Interface) specification describes the exit situation of the Java virtual machine when using the JNI Invocation API to load or unload the Java virtual machine.

Guess you like

Origin blog.csdn.net/picktheshy/article/details/132417569