"JVM Series" Chapter 1 -- JVM and Java Architecture

What is a virtual machine

A virtual machine is a virtual computer. It is a piece of software that executes a series of virtual computer instructions. In general, virtual machines can be divided into system virtual machines and program virtual machines .

  • The famous Visual Box and VMware belong to system virtual machines. They are completely simulations of physical computers and provide a software platform that can run a complete operating system.
  • A 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

A Java virtual machine is a virtual computer that executes Java bytecodes . It has an independent operating mechanism, and the Java bytecodes it runs are not necessarily compiled from the Java language. Various languages ​​of the JVM platform can share the cross-platform, excellent garbage collector, and reliable just-in-time compiler brought by the Java virtual machine.

The core of Java technology is the Java Virtual Machine (JVM, Java Virtual Machine) , because all Java programs run inside the Java Virtual Machine.

The role of the Java virtual machine: The
Java virtual machine is the operating environment of the binary bytecode, which is responsible for loading the bytecode into it, and interpreting/compiling it into machine instructions for execution on the corresponding platform. Each Java instruction is defined in detail in the Java Virtual Machine Specification, such as how to fetch operands, how to process operands, and where to put the processing results.

Features of the Java Virtual Machine:

  • Compile once, run everywhere
  • Automatic memory management
  • Automatic garbage collection function

The JVM runs on the operating system, it has no direct interaction with the hardware:
insert image description here
Where the JVM is located:
insert image description here
JDK, JRE and JVM
insert image description here

JDK: Abbreviation for Java Development Kit, java development kit, is a product of Sun Microsystems for Java developers, provides a java development environment and runtime environment, JDK contains JRE, you can find a jre directory in the JDK installation directory .

JRE: Short for Java Runtime Environment, it is an indispensable runtime environment for running programs written in the Java language. Through it, Java developers are able to release their own programs to users, allowing users to use them. There are bin and lib folders in the jre file. We can think that the bin folder corresponds to the JVM, and lib corresponds to the class library required for the JVM to work.

JVM: java virtual machine for short, is the java virtual machine we often say, it is the core part of the entire java to achieve cross-platform, all java programs will first be compiled into . Execute on the machine. The JVM shields the information related to the specific operating system platform, so that the Java program only needs to generate the object code (bytecode) running on the Java virtual machine, and it can run on various platforms without modification.

You need to run java programs, just install JRE, you
need to write java programs, you need to install JDK

Java code execution flow:

-> The java source program compiles the javac file (.java) through the compiler (javac.exe)
–> Generate bytecode file (.class)
—> The class loader loads the bytecode into memory and puts it into the method area of ​​the runtime data area
-----> The execution engine reads the bytecode and executes it segment by segment
------> Interpretation and execution (line-by-line interpretation of bytecode instructions) Compilation and execution (compiling hot code into machine instructions)
-------> Execute on the operating system (Windows, Linux, etc.)
insert image description here

insert image description here

What is a bytecode file?
The Java bytecode we usually say refers to the bytecode compiled into the Java language. To be precise, any bytecode format that can be executed on the jvm platform is the same, and is collectively referred to as: jvm bytecode

Different compilers can compile the same bytecode file, and the bytecode file can also run on different JVMs.

The Java virtual machine is not necessarily related to the Java language . It is only associated with a specific binary file format (class file) format. The class file contains the Java virtual machine instruction set (bytecode, Bytecodes) and symbol table, as well as some other auxiliary information.

JVM cross-language platform

With the official release of Java 7, the designers of the Java virtual machine have basically realized running programs written in non-Java languages ​​on the Java virtual machine platform through the JSR-292 specification.

The Java Virtual Machine doesn't care at all what programming language the programs running inside it are written in, it only cares about "bytecode" files. That is to say, the Java virtual machine is language-independent and will not simply be "lifetime bound" with the Java language, as long as the compilation results of other programming languages ​​satisfy and include the Java virtual machine's internal instruction set, symbol table, and other auxiliary information , he is a valid bytecode file that can be recognized by the virtual machine and loaded and run.
insert image description here
Multi-language mixed programming: Multi-language mixed programming
on the Java platform is becoming the mainstream, and understanding problems in a specific field through a language in a specific field is a direction for current software development to respond to increasingly complex project requirements.

In project development, the implementation of each layer can use different languages, the interface is transparent to developers of each layer, and there is no difficulty in the interaction between various languages, just as convenient as using the native API of your own language. Because they all end up running on a virtual machine.

JVM architecture

, insert image description here
HotSpot VM is one of the representative works of high-performance virtual machines on the market. It adopts the structure of coexistence of interpreter and real-time compiler.

The JVM consists of the following parts:

  • Class Loader (ClassLoader): Java's dynamic class loading functionality is handled by the ClassLoader subsystem. It loads, links. And initialize the class file when the class is first referenced at runtime (not compile time).
  • Runtime Data Area: The Java virtual machine divides the memory it manages into several different data areas during the execution of a Java program.
  • Execution Engine: The bytecode assigned to the runtime data area will be executed by the execution engine, which reads the bytecode and executes it segment by segment.
  • Native Interface: JNI will interact with the native method library and provide the native library required by the execution engine
  • Native Libraries: A collection of native libraries, required for execution by the execution engine

Three parts of the class loader: loading (Loading), linking (Linking), initialization (Initialization)
runtime data area Five parts: method area/meta space (Method Area), heap area (Heap Area), stack area (Stack Area) , Program Counter (PC Registers), Native Method Stack (Native Method statcks)
Three parts of execution engine: Interpreter, JIT Compiler, Garbage Collection

Detailed picture:
insert image description here

JVM Architecture Model

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

Features of stack-based architecture:

  • Simpler to design and implement, suitable for resource-constrained systems;
  • Avoid the problem of register allocation: use zero-address instruction allocation.
  • 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 easier to implement.
  • No hardware support required, better portability, better cross-platform implementation

Features of register-based architecture:

  • Typical applications are the x86 binary instruction set: such as the traditional PC and Android's Davlik virtual machine.
  • The instruction set architecture is completely dependent on hardware and has poor portability
  • Excellent performance and more efficient execution
  • Take fewer instructions to complete an operation.
  • In most cases, the instruction set based on register architecture is often dominated by one-address instructions, two-address instructions and three-address instructions, while the instruction set based on stack architecture is dominated by zero-address instructions.

Summary: Due to the cross-platform design, Java's instructions are designed according to the stack. Different platforms have different CPU architectures, so they cannot be designed to be register-based. The stack is relative to the register: it is cross-platform, the instruction set is small, the compiler is easy to implement, and there are many instructions; but the execution performance is worse than the register, and more instructions are required to achieve the same function.

Example: Execute 2+3 operation
stack-based calculation process (take Java virtual machine as an example):
iconst_2 //Constant 2 is pushed to the stack
istore_1
iconst_3 //Constant 3 is pushed to the stack
istore_2
iload_1
iload_2
iadd //Constant 2/3 is pushed to the stack, and the addition is performed
istore_0 // The result 5 is pushed to the stack

And the register-based calculation flow:
mov eax,2 //Set the value of the eax register to 1
add eax,3 //Add 3 to the value of the eax register

JVM life cycle

The startup of the virtual machine: The startup
of the Java virtual machine is accomplished by creating an initial class by the bootstrap class loader, which is specified by the specific implementation of the virtual machine.

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

Exit of the virtual machine:

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

JVM development major events

insert image description hereinsert image description here

JVM development history

Sun Classic VM: As early as 1996, when Java 1.0 was released, it was released by Sun and was the first commercial Java virtual machine in the world . It was completely eliminated when JDK 1.4. This virtual machine only provides an interpreter inside.

Exact VM: In order to solve the problem of the last virtual machine, when jdk1.2, sun provided this virtual machine.

  • Exact Memory Management: accurate memory management
  • Dimensions with modern high-performance virtual machines
  • It is only used for a short time on the solaris platform, and it is still a classic vm on other platforms.

HotSpot VM (emphasis)

HotSpot History:
Originally designed by a small company called "Longview Technologies". In 1997, the company was acquired by sun; in 2009, Sun was acquired by Oracle.
When JDK1.3, HotSpot VM became the default virtual machine

At present, Hotspot occupies an absolute market position and dominates the martial arts:

  • Whether it is JDK6, which is still widely used, or JDK8, which is widely used, the default virtual machine is HotSpot
  • Default virtual machine for Sun/oracle JDK and openJDK
  • There are applications from server, desktop to mobile and embedded.

HotSpot in the name refers to its hot spot code detection technology:

  • Find the most compiling valuable code through the counter, trigger just-in-time compilation or on-stack replacement
  • Through the cooperative work of the compiler and interpreter, the optimal program response time and the best execution performance are balanced

JRockit: Focus on server-side applications , it can pay less attention to the program startup speed, so JRockit does not contain parser implementation, all code is compiled and executed by the real-time compiler.

Extensive industry benchmarks show that the JRockit JVM is the fastest JVM in the world: with JRockit products, customers have experienced significant performance improvements (some over 70%) and hardware cost reductions (up to 50%).

Benefit: Comprehensive portfolio of Java runtime solutions

  • JRockit's solution for latency-sensitive applications JRockit Real Time provides JVM response time in milliseconds or microseconds, suitable for the needs of financial, military command, and telecommunication networks
  • The MissionControl suite of services, a set of tools to monitor, manage, and analyze applications in production with minimal overhead.

In 2008, JRockit was acquired by oracle. Oracle expresses the work of integrating two excellent virtual machines, which is roughly completed in JDK8. The way of integration is to transplant the excellent features of JRockit on the basis of HotSpot.

IBM's J9: IBM's J9, the full name of IBM Technology for Java Virtual Machine, referred to as IT4J, internal code: J9

The market positioning is close to that of HotSpot, and multi-purpose VMs such as server-side, desktop applications, and embedded are widely used in various IBM Java products. At present, it is one of the three influential commercial virtual machines, and it is also known as the fastest Java virtual machine in the world.

Around 2017, IBM released the open source J9VM, named openJ9, which was managed by the EClipse Foundation, also known as Eclipse OpenJ9.

KVM and CDC / CLDC Hotspot: Oracle's two virtual machines in the Java ME product line are: CDC/CLDC HotSpot Implementation VM KVM (Kilobyte) is an early product of CLDC-HI. Currently, it has an embarrassing position in the mobile field. Smart machines are divided into two parts by Angroid and ioS. world.

KVM is simple, lightweight, highly portable, and maintains its own market for lower-end devices:

  • Smart Controllers, Sensors
  • Mobile phones for the elderly, feature phones in underdeveloped areas

Azul VM: The first three "high-performance Java virtual machines" are used on general hardware platforms. Here Azu1VW and BEALiquid VM are proprietary virtual machines that are bound to specific hardware platforms and cooperate with hardware and software: fighters in high-performance Java virtual machines .

Azul VM is an ava virtual machine that Azu1Systems has improved on the basis of HotSpot and runs on Azul Systems' proprietary hardware Vega system.

Each Azu1VM instance can manage hardware resources of at least tens of CPUs and hundreds of GB of memory, and provides excellent features such as a garbage collector that achieves controllable GC time in a huge memory range, and thread scheduling optimized by proprietary hardware.

In 2010, AzulSystems began to shift from hardware to software and released its own zing JVM, which can provide features close to the Vega system on a general-purpose x86 platform.

Liquid VM: A fighter in a high-performance Java virtual machine. Developed by BEA, it runs directly on its own Hypervisor system

Liquid VM is now JRockit VE (Virtual Edition), it does not require the support of the operating system, or it implements the necessary functions of a dedicated operating system by itself, such as thread scheduling, file system, network support, etc.

With the termination of development of the JRockit virtual machine, so did the Liquid vM project.

Apache Marmony: Apache has also launched Apache Harmony, a Java operating platform compatible with JDK1.5 and JDK1.6.

It is an open source JVM jointly developed by IElf and Inte1. It was suppressed by the same open source openJDK. Sun was determined not to let Harmony obtain JCP certification. It was finally retired in 2011, and IBM turned to participate in OpenJDK.

Although there is no large-scale commercial use of Apache Harmony, its Java class library code has been absorbed into the Android SDK.

Micorsoft JVM: Microsoft developed the Microsoft JVM to support Java Applets in the IE3 browser. The virtual machine can only run under the windows platform. But it was the Java VM with the best performance under Windows at that time.

In 1997, Sun successfully accused Microsoft of trademark infringement and unfair competition, and lost a lot of money to Sun. Microsoft windowsXPSP3 erased its VM. Now the jdk installed on windows is HotSpot.

Taobao JVM: Released by the AliJVM team. Ali, the most powerful company using Java in China, covers many fields such as cloud computing, finance, logistics, e-commerce, etc. It needs to solve the complex problems of high concurrency, high availability and distribution. There are tons of open source products out there.

Based on openJDK, we developed our own customized version of AlibabaJDK, referred to as AJDK. It is the cornerstone of the entire Alibaba Java system. The first optimized, deeply customized and open source high-performance server version Java virtual machine in China based on openJDK Hotspot VM.

  • The innovative GCIH (GCinvisible heap) technology implements off-heap, that is, moving Java objects with a long life cycle from the heap to outside the heap, and Gc cannot manage Java objects inside GCIH, so as to reduce the frequency of GC recycling and The purpose of improving the recovery efficiency of Gc.
  • Objects in GCIH can also be shared among multiple Java virtual machine processes
  • Use crc32 instruction to implement JvM intrinsic to reduce JNI call overhead
  • Java profiling tool and diagnostic assistance for PMU hardware
  • ZenGc for big data scenarios

Taobao vm application has high performance on Ali products, and the hardware relies heavily on inte1 cpu, which loses compatibility, but improves performance. Currently, it has been launched on Taobao and Tmall, and the official Oracle JvM version has been replaced.

Dalvik VM: developed by Google, applied to Android system, and provided JIT in Android 2.2, which is developing rapidly.

Dalvik VM can only be called a virtual machine, not a "Java virtual machine". It does not follow the Java virtual machine specification and cannot directly execute Java's Class files.
The virtual machine is based on the register architecture, not the stack architecture of the jvm, and executes the compiled dex (Dalvik Executable) file. The execution efficiency is relatively high.

  • The dex (Dalvik Executable) files it executes can be converted from class files, applications are written using Java syntax, and most Java APIs can be used directly.

Android 5.0 replaces the Dalvik VM with the ART VM that supports Ahead of Time Compilation (AoT).

Graal VM: In April 2018, Oracle Labs announced GraalvM, known as "Run Programs Faster Anywhere" , with great ambitions. It echoes the "write once, run anywhere" of java in 1995.

GraalVM is a cross-language full-stack virtual machine enhanced on the basis of HotSpot VM, which can be used as a running platform for "any language". Languages ​​include: Java, Scala, Groovy, Kotlin; C, C++, Javascript, Ruby, Python, R, etc. At the same time, it supports mixing interfaces and objects of each other in different languages, and supports the use of native library files that have been written in these languages.

The working principle is to convert the source code of these languages ​​or the intermediate format of the source code compiled by the interpreter into an intermediate representation that can be accepted by the Graal VM. Graal VM provides the Truffle toolset to quickly build interpreters for a new language. It can also perform just-in-time compilation optimization at runtime to obtain better execution efficiency than native compilers.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324113638&siteId=291194637