In-depth understanding of what the Java virtual machine is

What is Java Virtual Machine

 

As a Java programmer, we write Java code every day, and the code we write is executed on something called the Java Virtual Machine. But if you want to ask what is a virtual machine, I am afraid many people will be ambiguous. In this article, I will write about my understanding of virtual machines. Due to limited capacity, there may be insufficient descriptions in some places. If you have a different understanding, welcome to exchange.

We all know that java programs must run on a virtual machine. So what exactly is a virtual machine? First look at the more reliable explanations found on the Internet:

A virtual machine is an abstract computer that is implemented by simulating various computer functions on an actual computer. The Java virtual machine has its own complete hardware architecture, such as processor, stack, registers, etc., and also has a corresponding instruction system. 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.

This explanation should be correct, but it only describes the external behavior and functions of the virtual machine, and does not explain the internal principles. Under normal circumstances, we don't need to know the operating principle of the virtual machine, as long as we focus on writing java code, this is the reason why the virtual machine exists - shielding the difference of the underlying operating system platform and reducing the complexity of development based on native language The nature of the Java language makes it possible to cross a variety of platforms (as long as the virtual machine manufacturer implements a virtual machine on a specific platform), and it is easy to use. These are all external characteristics of virtual machines, but to explain virtual machines from this information is too general to let us know the internal principles.


 

Explain the JVM from a process perspective

 

Let's try to understand virtual machines from an operating system level. We know that virtual machines run in the operating system, so what can run in the operating system? Of course it's a process, because a process is a unit of execution in an operating system. It can be understood in this way that when it is running, it is a process instance in the operating system, and when it is not running (stored in the file system as an executable file), it can be called a program.


Students who are familiar with the command line know that a command corresponds to an executable binary file. When you type this command and press Enter, a process will be created and the corresponding executable file will be loaded into the address space of the process. and execute its instructions. The following is a comparison of the HelloWorld program in C language and Java language to illustrate the problem.


First write the C language version of the HelloWorld program.

 

 

#include <stdio.h>

#include <stdlib.h>

 

int main(void) {

printf("hello world\n");

return 0;

}

Compile the C language version of the HelloWorld program:

 

 

gcc HelloWorld.c -o HelloWorld

 

Run the C language version of the HelloWorld program:

 

 

zhangjg@linux:/deve/workspace/HelloWorld/src$ ./HelloWorld

hello world


The file compiled by the gcc compiler is directly a binary executable file that can be recognized by the operating system. When we type the command ./HelloWorld on the command line, a process is created directly and the executable file is loaded into the process. In the address space of , execute the instructions in the file.

 

 

As a comparison, let's look at the compiled and executed form of the Java version of the HelloWord program.

First write the source file HelloWord.java:

 

 

public class HelloWorld {

 

public static void main(String[] args) {

System.out.println("HelloWorld");

}

}


Compile the Java version of the HelloWorld program:

 

zhangjg @linux : / deve / workspace / HelloJava / src $ javac HelloWorld.java

zhangjg@linux:/deve/workspace/HelloJava/src$ ls

HelloWorld.class HelloWorld.java

 

Run the Java version of the HelloWorld program:

 

zhangjg@linux:/deve/workspace/HelloJava/src$ java -classpath . HelloWorld

HelloWorld


As can be seen from the above process, when we run the Java version of the HelloWorld program, the command typed is not ./HelloWorld.class. Because class files are not binary executable files that can be directly recognized by the operating system. We typed in the command java. This command shows that we first start a program called java, which is a JVM process instance after running.

 

The execution flow of the above command is as follows:

The java command first starts the virtual machine process. After the virtual machine process is successfully started, it reads the parameter "HelloWorld", loads it into the memory as the initial class, and initializes and dynamically links the class (the initialization and dynamic linking of the class will be described later. Introduced in the blog), and then start execution from the main method of this class. That is to say, our .class file is not directly loaded by the system and executed on the cpu, but is hosted by a process called a virtual machine. First, the virtual machine process must be ready to start, and then the class loader in the virtual machine loads the necessary class files, including the basic classes in jdk (such as String and Object, etc.), and then the virtual machine process interprets the class bytecode instructions, put These bytecode instructions are translated into instructions that the native CPU can recognize before they can run on the CPU.

 

From this perspective, when executing a so-called java program, what is really executing is a process called the Java virtual machine, not the class files we wrote one by one. This process, called the virtual machine, handles some low-level operations, such as memory allocation and deallocation. The class file we write is just the "raw material" that the virtual machine process needs to execute. These "raw materials" are loaded into the virtual machine at runtime and interpreted and executed by the virtual machine to control the virtual machine to implement some relatively high-level operations defined in our java code, such as creating a file, etc. The information is regarded as control information for the virtual machine, that is, a virtual instruction.

 

Programming languages ​​also have their own principles. Learning a language is mainly to understand its principles. Seemingly a simple HelloWorld program, there is also a lot of in-depth content worth analyzing.

Introduction to JVM Architecture

In order to show the relationship between the virtual machine process and the class file, the following picture is specially drawn:

 

Please click here to enter image description

 

According to the content expressed in the above figure, the compiled class file is input into the Java virtual machine as the raw material of the Java virtual machine, so who will do this part of the work? In fact, inside the Java virtual machine, there is a subsystem called the class loader, which is used to load classes as needed at runtime. Note the words "as needed" in the sentence above. During the execution of the Java virtual machine, only when it needs a class, the class loader will be called to load the class, and not all classes will be loaded when it starts to run. It's like a person who only eats when he is hungry, instead of eating all his meals for a year at a time. In general, the time when the virtual machine loads a class is when a new class is used for the first time. Later articles in this column will discuss class loaders in Java in detail.


After the class loaded by the virtual machine is loaded into the memory of the Java virtual machine, the virtual machine reads and executes the bytecode instructions present in it. The part of the virtual machine that executes the bytecode instructions is called the execution engine. Just like a person, he is not finished after eating, but also needs to digest, and the execution engine is equivalent to the human gastrointestinal system. In the process of execution, each class file will be dynamically linked. The specific behavior of the execution engine and related content about dynamic linking will also be discussed in subsequent articles in this column.

 

We know that the Java Virtual Machine does automatic memory management. Specifically, it automatically frees unused objects without requiring the programmer to write code to free allocated memory. This part of the work is handled by the garbage collection subsystem.

 

It can be known from the above discussion that a Java virtual machine instance has three subsystems to ensure its normal operation during the running process, namely the class loader subsystem, the execution engine subsystem and the garbage collection subsystem. As shown below:

 

Please click here to enter image description

 

To run the virtual machine, the class file must be loaded and the bytecode instructions in the class file must be executed. It does so much and must need its own space. Just as what a person eats must first be placed in the stomach. Virtual machines also need space to store their data. First of all, the loaded bytecode needs a separate memory space to store; the execution of a thread also needs memory space to maintain the calling relationship of the method, store the data in the method and the intermediate calculation results; in the process of execution, it is impossible to Avoid creating objects that require a dedicated memory space to store them. The content of the virtual machine runtime data area will also appear in subsequent articles in this column. The runtime memory area of ​​a virtual machine can be roughly divided into several parts as shown in the following figure. (This is just a rough division, not a very fine division)

 

Please click here to enter image description

 

 

Summarize

 

Writing here, basically about my understanding of the java virtual machine is finished. Although the topic of this article is to understand the Java Virtual Machine in depth, you may feel that it is not "in-depth" at all, and it is only general. I feel the same way too. Due to my limited level, I can only do this. If you want to deeply understand the java virtual machine, it is strongly recommended to read three books:


"In-depth Java Virtual Machine"

"In-depth understanding of Java virtual machine JVM advanced features and best practices"

"Java Virtual Machine Specification"


In fact, I have also read these books, but their explanations of virtual machines are also based on an external model, without in-depth analysis of the internal implementation principles of virtual machines. The virtual machine is a big and complex thing. The people who implement the virtual machine are all powerful. If they have not participated in the implementation of the virtual machine, few people should be able to understand it. Some articles later in this column also refer to these three books. Although there are countless books that explain Java syntax, I have seen these three books so far, and there are not many online materials.

 

Finally make a summary:

1 A virtual machine is not mysterious, it is just an ordinary process from the point of view of the operating system.

2 This process called the virtual machine is special, it can load the class file we wrote. If the JVM is compared to a person, then the class file is the food we eat.

3 The class file is loaded by a subsystem called the class loader. Just like our mouths, we eat food into our stomachs.

4 The execution engine in the virtual machine is used to execute the bytecode instructions in the class file. Just like our stomach, it digests the food we eat.

5 During the execution of the virtual machine, it needs to allocate memory to create objects. When these objects become obsolete and useless, these useless objects must be cleaned up automatically. The task of cleaning up objects to reclaim memory is the responsibility of the garbage collector. Just like the food that people eat, after being digested, the waste must be excreted out of the body, making room for eating and digesting the food the next time you are hungry.

I think the article written by Xiaobian is good, and I am right. Please follow me, thank you for your likes.

Guess you like

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