Explain the Java virtual machine in a simple way-interview questions + collation of common knowledge points

The Java virtual machine is increasingly becoming an important part of the interviews of major Internet companies. For Java developers, only after understanding the principles behind Java can they write higher-quality code, and can quickly locate and find problems among intricate bugs. solution.

This article will first lead you to get started with the Java Virtual Machine, and then elaborate on the knowledge points frequently tested in the interview and the key technologies commonly used in development, to help everyone know what Java is and why it is.

Due to limited space, only a part is shown here. Friends who need the full version can click the link below.
Link: 1103806531 Code : CSDN

1. Introduction to Virtual Machine

1. Virtual machine concept

Virtual Machine (Virtual Machine) refers to a complete computer system with complete hardware system functions that is simulated by software and runs in a completely isolated environment. The work that can be done in the physical computer can be realized in the virtual machine. When creating a virtual machine in a computer, part of the hard disk and memory capacity of the physical machine needs to be used as the hard disk and memory capacity of the virtual machine. Each virtual machine has an independent CMOS, hard disk and operating system, and can operate the virtual machine just like a physical machine.

2. JVM virtual machine

JVM is the abbreviation of Java-Virtual-Machine, that is, Java Virtual Machine. JVM is a specification for computing devices. It is a fictitious computer that is realized by simulating various computer functions on an actual computer. .

3. JVM features

Insert picture description here

First of all, compiling and running everywhere is something that everyone knows when learning the Java language. In fact, it is not the Java language cross-platform, but the JVM cross-platform. When Jvm runs, it does not execute Java files, but executes compiled .class files.

Bytecode

Bytecode files are binary files that JVM can recognize and execute. After different programming languages ​​are compiled and processed by a compiler, they are converted into unified bytecode specification files so that JVM can execute.

Cross-platform

The cross-platform feature is that the JVM virtual machine can run on different computer systems, such as the frequently used Linux system, MacOS system, and Win system. This is how it is understood to compile once and run everywhere.

Cross language

With the continuous development and optimization of the JVM, many languages ​​rely on the capabilities of the JVM. Various programming languages ​​are compiled and converted into bytecode files, which can be recognized by the JVM. This is why business programming under the Java system is often mixed.

Note : The current and subsequent Jvm series articles are based on HotSpot-VM and JDK1.8+ version.

Second, the virtual machine structure

The overall structure of Jvm is roughly as follows:

Insert picture description here

1. Class loader

The class loader is used to load Java classes into the JVM virtual machine. The source code program .java file is converted into byte code .class file after being compiled by the compiler. The class loader is responsible for reading the byte code and converting it into java An instance of the .lang.Class class.

2. Runtime data area

Metadata area

The argument from JDK1.8, formerly known as Method-Area, stores data such as class information, constants, static variables, and code compiled by the just-in-time compiler that have been loaded by the virtual machine.

Heap area

A memory area shared by all threads, created when the virtual machine starts to store object instances.

JVM stack

You can refer to the data structure of the stack and store the memory model for the execution of Java methods. In Java development, the realization of a function requires the cooperation of multiple subroutine methods. When the program is executed, it will jump to the subroutine and store the address of the next instruction on the stack. After the subroutine is executed, the address will be taken out and returned to the original program.

Native method stack

The function of the native method stack is similar to that of the virtual machine stack, serving when the JVM calls the native method.

Program counter

A relatively small piece of memory space can be understood as the line number indicator of the bytecode executed by the current thread.

3. Execution engine

The core component of the Java virtual machine is bytecode input, bytecode analysis, and execution result output.

Three, life cycle

The JVM life cycle mentioned here refers to the cycle when the JVM executes a Java program:

Start-up initialization : The initial class is created by the boot class loader at startup;

Program execution : starting from the main method, execute the Java program until the end of the program execution;

Virtual machine exit : the program execution ends normally, or an exception or error occurs and it terminates, you can also call the exit exit method;

Four, HotSpot virtual machine

HotSpot is the most used virtual machine under the Java system. It combines the latest memory model, garbage collector and adaptive optimizer to provide the best performance for Java applications that use many advanced technologies.

The main reason: It is used a lot, and most of the Java runtime environments rely on the HotSpot virtual machine.

At last

Hope this article is helpful to everyone!

Due to limited space, the common knowledge points and interview questions of interviews are not shown in this article, but I have compiled them into documents, including a full set of video tutorials for architects and systematic materials about java. Friends in need can Click the link below to receive

Link: 1103806531 Password: CSDN

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_48655626/article/details/108732950