This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

JVM and Java Architecture

1.1, the preface

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 understanding of Java virtual machine (JVM), the core of Java technology, except that they will use various sophisticated technologies related to the Java platform in their projects. Some developers with certain working experience believe that SSM, micro-service and other upper technologies are the focus, while basic technologies are not important. In fact, this is a kind of “sick” put 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.

1.3. What do architects think about every day?

  • How can I make my system faster?
  • How to avoid system bottlenecks?
  • Participate in performance optimization and reconstruction of existing systems to ensure platform performance and stability
  • According to the business scenario and requirements, decide the technical direction and make the technical selection
  • Able to independently construct and design high-concurrency distributed solutions for massive data, meeting functional and non-functional requirements
  • Solve all kinds of potential system risks, core function architecture and coding
  • Analyze the bottleneck of the system, solve all kinds of problems, performance tuning, etc

1.4. Why learn about the JVM

  • Interview needs (BATJ, TMD, PKQ, etc.)

  • Essential skills for middle and advanced programmers: project management, tuning requirements

  • In pursuit of geek spirit: garbage collection algorithms, JIT, underlying principles

1.5. JVM: Cross-language platform

Java is one of the most widely used software development platforms. It is a cross-platform language, whereas the JVM is a cross-language platform. 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 almost becameOpen sourcePronoun of.
    • 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.

With the official release of Java7, Java virtual machine designers implemented the jSR-292 specification to basically run programs written in non-Java language on the Java virtual machine platform. The Java virtual machine doesn’t care what programming language the programs running inside it are written in, it only cares about bytecode files. For example, the milk we drink every morning, we don’t care about the breed and color of the cow behind it, but only about whether the milk is bad, good to drink, and balanced.

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. 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

1.6. 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 called: 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.

1.7. Multi-language mixed 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. We can imagine a project in which parallel processing is written in Clojure, the presentation layer is JRuby/Rails, the middle layer is Java, each application layer is done in a different programming language, and the interfaces are transparent to the 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.

1.8. 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 and Java HotSpot Virtual Machine was officially released as the default Virtual Machine for Java.
  • In 2002, JDK1.4 was released, and the old Classic virtual machine disappeared.
  • Scala for the Java platform was released in late 2003, the same year Groovy joined the Java camp.
  • In 2004, JDK1.5 was released. At the same time, JDK1.5 is renamed to JavaSE5.0.
  • In 2006, JDK6 was released. That same year, Java was open-source and OpenJDK was created. Not surprisingly, the Hotspot VIRTUAL machine has also become the default virtual machine in openJDK.
  • In 2007, the Java platform received a new partner, Clojure.
  • In 2008, Oracle acquired BEA and acquired 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.
  • In 2010, Oracle acquired Sun, acquired the Java trademark and the most valuable 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 JDk1.7U4, the new garbage collector G1 is officially enabled.
  • In 2017, JDK9 was released. Make G1 the default Gc instead of CMS
  • That same year, IBM made J9 Open source, forming what is now the 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
  • In the same year, JDK11 was released, the LTS version of the JDK, with revolutionary ZGC and tweaking JDK licensing
  • In 2019, JDK12 was released, joining The Shenandoah GC led by RedHat

OpenJDK and OracleJDK

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. So JDK11 is the child of the next version.

1.10. VMS and Java VMS

1.10.1. Virtual Machines

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:

  1. System Virtual machines: The famous Visual Box and Mware are system virtual machines. They are complete simulations of physical computers and provide a software platform that can run a complete operating system.
  2. Program virtual machine: The typical representative of 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.

1.10.2. 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), because all Java programs run inside the Java Virtual Machine.

Its role is that the Java virtual machine is the runtime environment for binary bytecode, which is responsible for loading bytecode into its interior and interpreting/compiling it for execution by machine instructions 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.

His characteristics:

  1. Compile once, run everywhere.
  2. Automatic memory management.
  3. Automatic garbage collection.

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

1.11. Overall structure of the JVM

HotSpot VM is one of the representative high performance virtual machines on the market at present. It adopts the architecture of interpreter and instant compiler. Today, the running performance of Java program has been completely changed, and has reached the point that it can compete with C/C++ program.

In multithreading, method areas and heap data are shared, while the Java stack, local method stack, and program counters are unique to each thread.

1.12 Java language implementation steps

1.13. Structural model of JVM

The flow of instructions entered by the Java compiler is basically of two kinds:

  1. A stack – based instruction set architecture.
  2. Another instruction set architecture is register-based instruction set architecture.

1.13.1 Stack-based instruction set architecture

His characteristics are four:

  • Simpler design and implementation, suitable for resource-constrained systems (embedded devices, small devices such as set-top boxes, printers).

  • 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.

1.13.2 Register-based instruction set architecture

  • 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

1.14. JVM lifecycle

1.14.1. Vm Startup

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.

1.14.2. 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.

1.14.3 Exiting a VM

The FOLLOWING conditions will cause the VM to exit:

  • 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.

1.15. Development history of the JVM

Sun Classic VM

Back in 1996, when Java1.0 was released, Sun released a Java virtual machine called Sun classic VM, which was also the world’s first commercial Java virtual machine. It was completely phased out in JDK1.4. , this virtual machine only provides an interpreter. 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

Exact Memory Management: non-conservative /Accurate Memory Management. To solve the problem of the last vm, Sun provided this vm in jdk1.2. The simulator can know exactly what type of data a location in memory is. It has the dimensions of a modern high-performance virtual machine:

  1. Hot spot detection

  2. Compilers and interpreters mix working modes

It was only used for a short time on Solaris, but it was still a Classic VM on other platforms, so it got short of breath and was eventually replaced by Hotspot VIRTUAL machine.

HotSpot VM

It was originally designed by a small company called Longview Technologies, which was acquired by Sun in 1997; Sun was acquired by Oracle in 2009. In JDK1.3, HotSpot VM became the default VIRTUAL machine and HotSpot dominates the wulin market.

The default virtual machine is HotSpot, Sun/Oracle JDK and OpenJDK in JDK6, which is still widely used, and JDK8, which is widely used. It has applications from server, desktop, mobile and embedded.

The name HotSpot refers to its HotSpot code detection technology. Use counters to find the most compilable code, trigger just-in-time compilation or on-stack substitution, and work with the compiler and interpreter to balance optimal program response time with optimal execution performance.

JRockit

It is focused on server-side applications, which can be implemented with little concern for startup speed, so JRockit does not include a parser implementation, and all code is compiled and executed by a 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. Its strength lies in its comprehensive portfolio of Java runtime solutions.

JRockit Real Time provides JVM response times in milliseconds or microseconds for financial, military command, and telecommunications networks.

It includes the MissionControl service suite, which is a set of tools to monitor, manage, and analyze applications in a production environment with minimal overhead. Basically, it’s a surveillance.

In 2008, JRockit was acquired by Oracle, and Oracle expressed the work of integrating the two great virtual machines, roughly done in JDK8. The way to integrate is to port the good features of JRockit on top of HotSpot.

J9

The full name of J9: IBM Technology for Java Virtual Machine, IT4J for short, internal code: J9. Market positioning is close to HotSpot, server side, desktop application, embedded and other multi-purpose VM, 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) is an early product of CLDC-HI. Currently, it is in an awkward position in the mobile space. Smartphones are divided between Android and iOS, and it has no market at all.

KVM is simple, lightweight, highly portable, and maintains its niche on lower-end devices:

  1. Intelligent controller, sensor.

  2. Mobile phones for the elderly and feature phones in economically underdeveloped areas.

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. It is also the fighter of high performance Java virtual machines.

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

It was developed by BEA corporation and runs directly in a high-performance Java virtual machine (JVM) on their 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 Internet Explorer 3, which runs only 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. Alibaba is the most powerful Java company in China, covering cloud computing, finance, logistics, e-commerce and many other fields. It needs to solve the complex problems of high concurrency, high availability and distribution. There are plenty of open source products out there. He developed his own custom version AlibabaJDK, or AJDK for short, based on OpenJDK. It is the cornerstone of 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, that is, Java objects with a long life cycle are moved from the heap to the outside of the heap, and the GC cannot manage Java objects inside the GCIH, so as to reduce the recycle frequency of GC and improve the recycle efficiency of GC. Objects in GCIH can also be shared across multiple Java virtual machine processes, JVM intrinsic reductions in JNI call overhead are implemented using crC32 directives, as well as Java profiling tool and diagnostic assistance for PMU Hardware and ZenGc for big data scenarios

Taobao VM application has high performance on Ali products. The hardware is heavily dependent on Intel CPU, resulting in loss of compatibility but improved performance. Currently, taobao VM has been launched on Taobao and Tmall, and all official JvM versions of Oracle have been replaced.

Dalvik VM

It was developed by Google, applied to Android, and provides JIT in Android2.2, which 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. It is register-based, not JVM stack, and executes compiled dex (Dalvik Executable) files. 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 different languages to mix each other’s interfaces and objects, support these languages to use already written local library files.

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.