Introduction to the

The JVM, also known as the Java Virtual Machine, is the basis for running Java programs and is responsible for converting Java Bytecodes into Machine code suitable for running in various operating systems. Today we walk into the core JVM of Java and appreciate the philosophy of Java in design.

The JVM is a standard

Brother F, we often hear about hotspot VM. What is the relationship between hotspot VM and JVM?

In fact, the JVM is just a standard, like a protocol, and anything that implements and satisfies this protocol can be called a JVM. Of course, Java is now owned by Oracle, so these so-called JVM standards are also issued by Oracle. If you look at the Oracle documentation, you will find a section called Java SE Specifications that lists the implementation standards for the JVM. The latest standard is The Java Virtual Machine Specification, Java SE 14 Edition.

More highlights:

  • Blockchain from getting started to Giving up series tutorials – with ongoing updates covering cryptography, Hyperledger, Ethereum,Libra, Bitcoin and more
  • Spring Boot 2.X Series tutorials: Learn Spring Boot from Scratch in seven days – continuous updates
  • Spring 5.X Series tutorials: Everything you can think of in Spring5 – constantly updated
  • Java Programmer from Handyman to Expert to God (2020 edition) – Ongoing updates with detailed articles and tutorials

For more, visit www.flydean.com

Since the JVM is a standard, there are many possible implementations. There are many different versions developed by companies that meet the JVM standards.

Here’s a comparison of the current JVMS that I picked up from Wikipedia:

Brother F, isn’t it better for us to make a JVM together? Why make the same wheel over and over again?

Have you heard about the API 10-year lawsuit between Oracle and Google? You can’t even use an API, let alone a JVM. Each vendor ended up with so many versions of the JVM for their own benefit.

The most commonly used of these JVMS is the HotSpot JVM, after all it is the son of Oracle, or the standard for JVMS.

Next up is Eclipse OpenJ9, an IBM-led JVM that is typically only used with IBM products due to licensing restrictions.

The execution order of Java programs

To illustrate what the JVM does, let’s review the order in which Java programs are executed.

  1. Write Java code files such as example.java
  2. Compile the source file into example.class using the Java compiler javac
  3. The JVM loads the generated bytecode files and converts them into native machine code that the machine can recognize for execution

The JVM architecture

F: There are so many features of the Java language, and all of them have to run in the JVM. Isn’t the architecture of the JVM particularly complicated? I’m afraid I don’t understand.

In fact, the JVM can be divided into three parts, five Spaces and three engines. It is not too complicated to explain, but take a look at the overall JVM architecture diagram below.

From the diagram above, you can see that there are three major parts of the JVM: the classloading system, the runtime data area, and the Execution Engine.

Class loading system

The class loading system is divided into three phases: loading, linking, and initialization.

Loading as you know, there is a special ClassLoader in Java that does this. In addition to loading classes, a ClassLoader can also be used to load resources.

Before JDK9, there were three classloaders by default:

  1. Bootstrap ClassLoader

This ClassLoader mainly loads rt.jar under /jre/lib, and this ClassLoader is written in C/C++, and is the parent of the Extension ClassLoader. Correct me if I am wrong! .

  1. Extension ClassLoader

Launcher$ExtClassLoader this class loads the JDK extension /jre/lib/ext. Its implementation is sun.misc.Launcher$ExtClassLoader:

static class ExtClassLoader extends URLClassLoader {
        private static volatile Launcher.ExtClassLoader instance;

        public static Launcher.ExtClassLoader getExtClassLoader(a) throws IOException {
            if (instance == null) {
                Class var0 = Launcher.ExtClassLoader.class;
                synchronized(Launcher.ExtClassLoader.class) {
                    if (instance == null) { instance = createExtClassLoader(); }}}return instance;
        }
Copy the code

Let’s take a look at its implementation. It actually creates a singleton using double-checked locking, and we can consider how to re-initialize the placeholder class using lazy initialization.

  1. System ClassLoader

This loader loads classes defined in the ClassLoader. The implementation class is sun.misc.Launcher$AppClassLoader. The implementation of this class is too long to be listed completely here:

static class AppClassLoader extends URLClassLoader 
Copy the code

After JDK9, there are three built-in class loaders: BootClassLoader, PlatformClassLoader, and AppClassLoader:

    private static class BootClassLoader extends BuiltinClassLoader {
        BootClassLoader(URLClassPath bcp) {
            super(null.null, bcp);
        }

        @Override
        protectedClass<? > loadClassOrNull(String cn,boolean resolve) {
            return JLA.findBootstrapClassOrNull(this, cn); }};Copy the code
private static class PlatformClassLoader extends BuiltinClassLoader 
Copy the code
private static class AppClassLoader extends BuiltinClassLoader
Copy the code

In the Linking stage, three things are mainly done:

  1. Verification – Verifies that the structure of the bytecode file is correct, or raises LinkageError if it is incorrect.
  2. Preparation – Responsible for creating static fields and initializing their values.
  3. Resolution – The process of replacing classes, interfaces, fields, and methods referenced in a type’s constant pool with direct references.

During the Initialization phase, we call the parent class and its Initialization methods to set the Initialization value of a variable.

Runtime data area

The class is loaded, initialized, and ready to run.

Runtime allocates space for data, which is what the runtime data area is for.

The runtime data area can be divided into five sections:

  1. Method Area

A method area is a non-heap memory space that is used to store class structures, static fields, method, Method’s data, and static fields. The method area is created at JVM startup and is shared across all threads.

The run-time Constant Pool is placed in the method area, which is the runtime representation of the constant_pool in the class file.

Note that prior to JDK8, the implementation of the method area in HotSpot JVM was called persistent Perm Gen. However, after JDK8, Perm Gen has been cancelled and is now called Metaspace. Metaspace is not in the Java VIRTUAL machine; it uses local memory. Metaspace can be controlled by -xx :MaxMetaspaceSize.

  1. Heap Area

Heap areas mainly store class objects and arrays. The garbage collector (GC) is mainly used to reclaim objects in the Heap Area.

  1. Stack Area

Because of the stack structure, this area is always LIFO(Last in first out). We consider the execution of a method, and when the method executes, a block is created in the Stack Area that holds references to local objects and other objects. Once the method completes execution, the block is removed from the stack for other methods to access.

  1. PC Registers

The PC Registers is used to track the execution status of the program, for example, saving the current execution address and the next address.

  1. Native Methods

Finally, there is the local method area, and since much of the underlying JVM is implemented in C/C++, the implementation of these methods constitutes the local method area.

Execution engine

The execution engine is responsible for translating Java bytecode into machine code and executing it.

Java bytecode is a Java bytecode, and you can find a compiled class and use JavAP to parse:

javap -v BufferUsage
Copy the code

I don’t want to describe the meaning of the output here, but we will explain it in detail in a later article.

Here we can see that each method has a snippet of Code called OpCode, which is the operation command that the JVM understands.

There are three parts in the execution engine:

  1. Interpreter

The translator is used to read the OpCode described above and translate it into machine language. Because translators need to translate bytecode command by command, they can be slow. This is where Java was criticized a long, long time ago.

  1. JIT (Just-In-Time) compiler

To address the problem of Interpreter slow translation, the JDK introduced the JIT, which improves execution efficiency by translating bytecode into machine code for frequently used code and reusing it directly.

  1. Garbage Collector

The GC is used to reclaim the Heap Area, which is a Daemon thread.

conclusion

This article presented the overall architecture information for the JVM. Details on each part will follow in a series of articles. Welcome to the series.

Author: Flydean program stuff

Link to this article: www.flydean.com/jvm-all-in-…

Source: Flydean’s blog

Welcome to pay attention to my public number: procedures those things, more wonderful waiting for you!