Note source: Silicon Valley JVM complete tutorial, millions of playback, the peak of the entire network (Song Hongkang details Java virtual machine)

Update: gitee.com/vectorx/NOT…

Codechina.csdn.net/qq_35925558…

Github.com/uxiahnan/NO…

[TOC]

1.JVM and Java architecture

1.1. Introduction

Have you ever been hurt as a Java engineer? Have you encountered any of these problems?

  • Running online system suddenly freezes, the system can not access, or even directly OOM
  • Wanted to solve the online JVM GC problem, but didn’t know where to start
  • New project launch, all kinds of JVM parameters set a blank face, directly default to it and then JJ
  • You have to recap the principles and concepts of the JVM before every interview, but the interviewer often asks you how to tune VM parameters in a real project, how to solve GC, OOM, etc

Most Java developers have little knowledge of the Java Virtual Machine, the core of Java technology, other than the various advanced technologies associated with the Java platform that they use in their projects.

How do developers view the upper-layer framework

Some developers with a certain amount of work experience believe that SSM, micro-services and other upper-layer technologies are the focus, while basic technologies are not important. This is actually a kind of “sickness” that puts the cart before the horse.

If we compare the core library API to a mathematical formula, then the knowledge of the Java Virtual Machine is like the derivation of the formula.

Computer systems are far away from us, and it is easy to write program code in a high-level language without understanding the underlying implementation. But computers don’t actually understand high-level languages

Why should we learn about the JVM?

  • Interview needs (BATJ, TMD, PKQ, etc.)
  • Essential skills for intermediate and senior programmers
    • Project management, tuning requirements
  • Pursue the spirit of the geek
    • For example: garbage collection algorithm, JIT, underlying principles

Java vs C++

The garbage collection mechanism takes care of a lot of tedious work for us and greatly improves the efficiency of development. However, garbage collection is not a panacea. Understanding the internal memory structure and working mechanism of JVM is the foundation for designing highly scalable applications and diagnosing runtime problems.

1.2. Audience and bibliography

1.3. Introduction to Java and JVM

TIOBE language Hot list:index | TIOBE – The Software Quality Company

Programming Language 2021 2016 2011 2006 2001 1996 1991 1986
C 1 2 2 2 1 1 1 1
Java 2 1 1 1 3 26
Python 3 5 6 8 27 19
C++ 4 3 3 3 2 2 2 8
C# 5 4 5 7 13
Visual Basic 6 13
JavaScript 7 8 10 9 10 32
PHP 8 6 4 4 11
SQL 9
R 10 17 31
Lisp 34 27 13 14 17 7 4 2
Ada 36 28 17 16 20 8 5 3
(Visual) Basic 7 6 4 3 3 5

There is no best programming language in the world, only the most suitable programming language for specific application scenarios

JVM: Cross-language platform

Java is one of the most widely used software development platforms. With the growth of Java and the Java community, Java is no longer simply a computer language, but a platform, a culture and a community.

  • As a platform, the Java Virtual Machine plays an important role
    • Groovy, Scala, JRuby, Kotlin, etc., are all part of the Java platform
  • As a culture, Java has almost become synonymous with “open source”.
    • Third-party open source software and frameworks. Such as Tomcat, Struts, MyBatis, Spring and so on.
    • Even the JDK and JVM themselves have many open source implementations, such as openJDK and Harmony.
  • As a community, Java has the largest number of technology advocates and open source community support in the world, with countless forums and materials. From desktop application software, embedded development to enterprise application, background server, middleware, you can see the figure of Java. The complexity of its application forms and the number of participants are also surprising.

Each language needs to be converted into a bytecode file, and the resulting bytecode file can be run and processed by the Java Virtual machine

  • With the official release of Java7, Java virtual machine designers through the JSR-292 specification basic implementation inThe Java virtual machine platform runs programs written in a language other than Java.
  • The Java virtual machine doesn’t care 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 has language-neutral, will not simply with the Java language “binding” for life, as long as other programming language to compile the results meet the internal instruction set and contains a Java virtual machine, the symbol table and other auxiliary information, it is a valid bytecode file, can be identified by the virtual machine and loading operation.

The bytecode

  • When we say Java bytecode, we mean bytecode compiled in the Java language. Exactly any bytecode format that can be executed on the JVM platform is the same. So it should be collectively referred to as:The JVM bytecode.
  • Different compilers can compile the same bytecode files, and bytecode files can run on different JVMS.
  • The Java Virtual machine is not necessarily related to the Java language, but to a particular binary file format, the Class file format, which contains the Java Virtual machine instruction set (also known as Bytecodes, Bytecodes) and symbol tables, along with other auxiliary information.

Multilingual hybrid programming

  • Multi-language hybrid programming on Java platform is becoming the mainstream, and solving domain-specific problems through domain-specific languages is a direction of software development to deal with increasingly complex project requirements.
  • Imagine a project in which parallel processing is written in Clojure, the presentation layer in JRuby/Rails, the middle layer in Java, each application layer in a different programming language, and the interfaces are transparent to developers at each layer.The languages interact with each other without any difficulty, just as easily as using native apis for your own language, because they all end up running on top of a single virtual machine.
  • System-level, low-level support for these languages that run on top of the Java Virtual machine, but outside of Java, is rapidly increasing, A series of projects and feature improvements (such as the Da Vinci Machine project, Nashorn engine, InvokeDynamic instructions, java.lang.Invoke package, etc.) centered around JSR-292,Promote the Development of Java VMS from Java language VMS to multi-language VMS.

How do YOU really understand the JVM?

The Java virtual machine is so complex that the best way to really understand how it works is to write one yourself!

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

Are things difficult or easy in the world?

For it, the sufferers are easy; If you do not, it is difficult to make things easy

1.4. Major events in the development of Java

  • In 1990, the Green Team, a Sun computer group led by Patrick Naughton, MikeSheridan, and James Gosling, developed a new programming language, oak, and later Java
  • Java made its public debut in 1995 with the official release of Sun’s Java and HotJava products.
  • On January 23, 1996, Sun Microsystems released JDK 1.0.
  • In 1998, JDK1.2 was released. At the same time, Sun published JSP/Servlet, EJB specifications, and the division of Java into J2EE, J2SE, and J2ME. This represents the beginning of Java’s march into enterprise, desktop, and mobile applications.
  • In 2000, JDK1.3 was released,Java HotSpot Virtual Machine has been released as the default Java Virtual Machine.
  • In 2002, JDK1.4 was released, and the old Classic virtual machine disappeared.
  • By the end of 2003,Scala for the Java platform was released, and Groovy joined the Java camp that same year.
  • In 2004, JDK1.5 was released. At the same time, JDK1.5 is renamed to JavaSE5.0.
  • In 2006, JDK6 was released. In the same year,Java is open source and OpenJDK is built. It makes sense,The Hotspot VIRTUAL machine has also become the default virtual machine in openJDK.
  • In 2007,The Java platform has a new partner, Clojure.
  • Oracle acquired BEA in 2008,You get the JRockit virtual machine.
  • In 2009, Twitter announced the migration of most of its backend applications from Ruby to Scala, another large-scale adoption of the Java platform.
  • Oracle bought Sun in 2010,Get the Java trademark and truest value HotSpot VIRTUAL machine. At this point, Oracle has HotSpot and JRockit, the two most popular virtual machines in the market, and plans to integrate them in the future: HotRockit
  • In 2011, JDK7 was released. In the JDK1.7 u4,The new garbage collector G1 has been officially enabled.
  • In 2017, JDK9 was released.Make G1 the default Gc instead of CMS
  • In the same year,IBM’s J9 is open source, forming the current Open J9 community
  • In 2018, Google awarded Oracle $8.8 billion in damages for Android’s Java infringement
  • That same year, Oracle made JavaEE history by donating JDBC, JMS, and Servlet to the Eclipse foundation
  • That same year, JDK11 was released, the LTS version of the JDK,Release revolutionary ZGC and tweak JDK licensing
  • In 2019, JDK12 was released, joining RedHat in leading developmentshenandoah GC

Prior to JDK11, there were some closed-source features in the OracleJDK that were not in the OpenJDK. But in JDK11, we can assume that the OpenJDK and OracleJDK code are virtually identical.

However, the mainstream JDK 8 was announced to be discontinued after January 2019. In addition, JDK 11 and later versions are no longer available with free long-term support (LTS), and JDK 15 and JDK 16 are not a long-term support, with the latest JDK 15 only available for six months until March 2021. Never use a non-long-supported version like JDK 15 in production.

1.5. VMS and Java VMS

The virtual machine

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

  • The famous Visual Box, Mware are system virtual machines, theyIt’s a complete simulation of a physical computer, provides a software platform that can run a complete operating system.
  • The typical example of a program virtual machine is the Java Virtual Machine, whichDesigned specifically to execute a single computer programThe 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.

The Java virtual machine

  • A Java virtual machine is a virtual computer that executes Java bytecodes. It has an independent running mechanism, and the Java bytecodes it runs may not be compiled from the Java language.
  • The languages of the JVM platform share the cross-platform features of the Java Virtual Machine, excellent garbage callback, and reliable just-in-time compilers.
  • The core of Java technology is the Java virtual machine(JVM, Java Virtual Machine), because all Java programs run inside the Java Virtual Machine.

role

  • The Java virtual machine is the runtime environment for binary bytecode, which 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 is defined in the Java Virtual Machine specification in detail, such as how to fetch operands, how to process operands, and where to put the results of processing.

The characteristics of

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

The location of the JVM

The JVM runs on top of the operating system and has no direct interaction with the hardware

1.6. The overall structure of the JVM

  • HotSpot VM is one of the representative high performance virtual machines in the market.
  • It uses an interpreter and just-in-time compiler architecture.
  • Today, The performance of Java programs has changed so much that it can compete with C/C++ programs.

1.7. Java code execution flow

1.8. Architectural model of the JVM

The instruction stream that the Java compiler inputs is basically a stack based instruction set architecture, and another instruction set architecture is a register based instruction set architecture.

Specifically: The differences between the two architectures:

Based on the characteristics of stack architecture

  • Simpler design and implementation, suitable for resource-constrained systems
  • Avoid the register allocation problem: use zero address instruction mode allocation
  • Most of the instructions in the instruction stream are zero-address instructions, whose execution depends on the operation stack. The instruction set is smaller and the compiler is easier to implement
  • No need for hardware support, better portability, better cross-platform implementation

Register-based architecture features

  • Typical applications are x86 binary instruction sets: traditional PCS and the Android Davlik virtual machine
  • Instruction set architecture is completely dependent on hardware and has poor portability
  • Excellent performance and more efficient execution
  • It takes less instruction to complete an operation
  • In most cases, the instruction set based on register architecture is usually dominated by one address instruction, two address instruction and three address instruction, while the instruction set based on stack architecture is dominated by zero address instruction

For example 1

The same logic operation 2+3 is performed, and its instructions are as follows:

Stack-based computing flow (Using the Java VM as an example) :

iconst_2 // push constant 2 to the stack
istore_1
iconst_3 // push constant 3 to the stack
istore_2
iload_1
iload_2
iadd // Constant 2/3 out of stack, perform add
istore_0 // result 5 is pushed into the stack
Copy the code

And register-based calculation process

mov eax,2 // Set the eAX register to 1
add eax,3 // Add 3 to the eAX register
Copy the code

For example 2

public int calc(a){
    int a=100;
    int b=200;
    int c=300;
    return (a + b) * c;
}
Copy the code
> javap -c Test.class
...
public int calc(a);
    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
}
Copy the code

conclusion

Due to its cross-platform design, Java’s instructions are designed on a stack basis. Different platforms have different CPU architectures, so they cannot be register-based. Advantages are cross-platform, small instruction set, compiler easy to implement, disadvantages are performance degradation, to achieve the same function requires more instructions.

Today, even though embedded platforms are no longer the dominant platform for Java applications (HotSpotVM is no longer hosted on embedded platforms), why not switch to a register-based architecture?

1.9. JVM lifecycle

Start the VM

The Java virtual machine is started by bootstrap Class Loader to create an initial class that is specified by the implementation of the virtual machine.

Vm execution

  • A running Java virtual machine has a clear job: executing Java programs.
  • He runs when the program begins and stops when it ends.
  • When a so-called Java program is executed, what is really being executed is a process called the Java Virtual Machine.

Exit the VM

There are several cases as follows:

  • The program completes normally
  • The program encounters an exception or error during execution and terminates abnormally
  • The Java virtual machine process was terminated due to an operating system activation 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 the exit or HALT operation.
  • In addition, the JNI (Java Native Interface) specification describes the Java Virtual machine exit when the Java Virtual machine is loaded or unloaded using the JNI Invocation API.

X. The history of the JVM

Sun Classic VM

  • Back in 1996, with version 1.0 of Java, Sun released a Java VIRTUAL machine called the Sun Classic VM, which also served as a Java virtual machineThe world’s first commercial Java virtual machineWas completely phased out in JDK1.4.
  • The virtual machine provides only an interpreter internally. There’s also a just-in-time compiler, so it’s inefficient, and the just-in-time compiler caches hot code, so it’s more efficient to use hot code in the future.
  • If you are using a JIT compiler, you need to do a plug-in. But once a JIT compiler is used, the JIT takes over the execution system of the virtual machine. The interpreter stops working. The interpreter and compiler do not work together.
  • This virtual machine is now built into hotspot.

Exact VM

  • To solve the previous virtual machine problem, Sun provided this virtual machine in jdk1.2.
  • Exact Memory Management: Exact Memory Management
    • Also called non-conservative /Accurate Memory Management
    • The virtual machine can know exactly what type of data is stored at a location in memory.
  • Provides the dimensions of modern high-performance VMS
    • Hot spot detection
    • Compilers and interpreters mix working modes
  • It was only used briefly on Solaris, and was still a Classic VM on other platforms
    • Hero short of breath, eventually replaced by Hotspot virtual machine

HotSpot VM

  • The HotSpot history
    • It was originally designed by a small company called Longview Technologies
    • The company was acquired by Sun in 1997; Sun was acquired by Oracle in 2009.
    • In JDK1.3, HotSpot VM becomes the default VM
  • At present Hotspot occupies the absolute market position and dominates the martial arts.
    • In both the still widely used JDK6 and the heavily used JDK8, the default VIRTUAL machine is HotSpot
    • Default virtual machine for Sun/Oracle JDK and OpenJDK
    • Therefore, the default virtual machine introduced in this course is HotSpot, and the related mechanism mainly refers to HotSpot Gc mechanism. (For example, neither of the other two commercial virtual machines has the concept of a method area.)
  • From server, desktop to mobile terminal, embedded applications.
  • The name HotSpot refers to its HotSpot code detection technology.
    • The counter finds the code with the most compile value, triggering either just-in-time compilation or on-stack replacement
    • The compiler and interpreter work together to balance optimal program response time with optimal execution performance

JRockit

  • Focus on server-side applications

    • It doesn’t care much about startup speed, so JRockit doesn’t include a parser implementation, and all the code is compiled and executed by the just-in-time compiler.
  • Numerous industry benchmarks show that the JRockit JVM is the fastest JVM in the world.

    • Customers have experienced significant performance improvements (some over 70%) and hardware cost reductions (up to 50%) using JRockit products.
  • Advantages: Comprehensive Java runtime solution portfolio

    • JRockit Real Time provides JVM response times in milliseconds or microseconds for financial, military command, and telecommunications networks
    • MissionControl service suite, which is a set of tools for monitoring, managing, and analyzing applications in a production environment with minimal overhead.
  • In 2008, JRockit was acquired by Oracle.

  • Oracle expresses the work of integrating two great virtual machines, roughly done in JDK8. The way to integrate is to port the good features of JRockit on top of HotSpot.

  • Gosling: Currently works at Google, working on artificial intelligence and underwater robotics

The IBM J9

  • Full name: IBM Technology for Java Virtual Machine, IT4J for short, internal code: J9

  • Market positioning close to HotSpot, server-side, desktop applications, embedded and other multi-purpose VM

  • It is widely used in various IBM Java products.

  • Currently, one of the three most influential commercial virtual machines also claims to be the fastest Java virtual machine in the world.

  • Around 2017, IBM released the open source J9VM, named openJ9, and handed it over to the EClipse Foundation, also known as EClipse openJ9

KVM and CDC/CLDC Hotspot

  • Oracle has two virtual machines in the Java ME product line: CDC/CLDC HotSpot Implementation VM

  • KVM (Kilobyte) was an early product of CLDC-HI

  • Mobile is in an awkward position right now, with smartphones being split between Android and iOS.

  • KVM is simple, lightweight, and highly portable, maintaining its niche on lower-end devices

    • Intelligent controller, sensor
    • Mobile phones for the elderly and feature phones in economically underdeveloped areas
  • Rule for all virtual machines: Compile once, run anywhere.

Azul VM

  • The first three “high performance Java virtual machines” are used on general-purpose hardware platforms. Here Azul VW and BEA Liquid VM are proprietary virtual machines that are tied to a specific hardware platform and work with hardware and software

    • Fighter in high performance Java Virtual machine.
  • Azul VM is a Java virtual machine that runs on Azul Systems’ proprietary hardware Vega system, a major improvement on HotSpot.

  • Each Azul VM instance can manage hardware resources with at least dozens of cpus and hundreds of GIGABytes of memory, and provides excellent features such as a garbage collector for manageable GC times in a large memory range, proprietary hardware-optimized thread scheduling, and more.

  • In 2010, AzulSystems began to shift from hardware to software, releasing its Zing JVM, which provides nearly Vega features on the general-purpose x86 platform.

Liquid VM

  • Fighter in high performance Java Virtual machine.

  • BEA corporation developed, directly run on their own Hypervisor system

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

  • With the JRockit virtual machine being discontinued, the Liquid vM project was discontinued.

Apache Harmony

  • Apache has also launched Apache Harmony, a Java runtime platform compatible with JDK1.5 and JDK1.6.

  • It was an open source JVM jointly developed by IBM and Intel. It was suppressed by the OpenJDK, which was also open source. Sun was determined not to allow Harmony to be certified by JCP, and it was finally retired in 2011, with IBM participating in OpenJDK instead

  • While there are no large-scale commercial cases of Apache Harmony, its Java library code has been incorporated into the Android SDK.

Micorsoft JVM

  • Microsoft developed the Microsoft JVM to support Java Applets in IE3.

  • It only runs on Windows. But it was the best-performing Java VM on Windows at the time.

  • In 1997, Sun successfully sued Microsoft for trademark infringement and unfair competition, which cost Sun a lot of money. Microsoft erased its VM in WindowsXP SP3. The JDK installed on Windows today is HotSpot.

Taobao JVM

  • Published by the AliJVM team. Ali, the most powerful Java company in China, covers cloud computing, finance, logistics, e-commerce and many other fields, and needs to solve the complex problems of high concurrency, high availability and distribution. There are plenty of open source products out there.

  • Based on OpenJDK, we developed our own customized version AlibabaJDK, or AJDK for short. Is the cornerstone of the entire Ali Java system.

  • The first optimized, deeply customized and open source high-performance server version Java VIRTUAL machine released based on OpenJDK Hotspot VM in China.

    • The innovative GCIH (GC Invisible Heap) technology implements off-heap,To reduce the recycle frequency of the GC and improve the recycle efficiency of the GC, a Java object with a long life cycle is moved from the heap to the heap and the GC cannot manage Java objects inside the GCIH.
    • In the GCIHObjects can also be shared across multiple Java virtual machine processes
    • Implement the JVM Intrinsic to reduce the overhead of JNI calls using crC32 instructions
    • Java Profiling Tool and diagnostic assistance for PMU Hardware
    • ZenGc for big data scenarios
  • Taobao VM application has high performance on Alibaba products. The hardware is heavily dependent on Intel CPU, resulting in loss of compatibility but improved performance

    • At present, it has been online on Taobao and Tmall and replaced all the official JvM versions of Oracle.

Dalvik VM

  • Developed by Google for Android and provided with JIT in Android2.2, it is growing rapidly.

  • The 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 execute Java Class files directly

  • Register-based architecture, not the JVM’s stack architecture.

  • The compiled dex (Dalvik Executable) file is executed. Execution efficiency is relatively high.

    • It executes dex (Dalvik Executable) files that can be converted from class files, uses Java syntax to write applications, and can directly use most Java apis.
  • Android 5.0 replaces the Dalvik VM with an ART VM that supports Ahead of Time Compilation (AoT).

Graal VM

  • In April 2018, Oracle Labs unveiled Graal VM, which it calls “Run Programs Faster Anywhere” and is very ambitious. This echoes Java’s “Write once, Run Anywhere” in 1995.

  • Graal VM is a cross-language full-stack VIRTUAL machine enhanced on top of HotSpot VM and can be used as a platform for “any language”. Languages include: Java, Scala, Groovy, Kotlin; C, C++, Javascript, Ruby, Python, R, etc

  • Support for mixing interfaces and objects in different languages using native library files that are already written

  • It works by taking the source code of these languages, or the compiled intermediate format of the source code, and translating it through the interpreter into an intermediate representation that can be accepted by Graal VM. Graal VM provides the Truffle toolset to quickly build an interpreter for a new language. At run time, the compiler can be optimized on the fly, which is more efficient than the native compiler.

  • If HotSpot is ever to be replaced, Graal VM has the best hope. But the Java software ecosystem hasn’t changed at all.

conclusion

Depending on the implementation, the memory structure of a particular JVM may differ from one vendor to another, or from one vendor to another. The Oracle HotSpot VM is the default virtual machine.