1. The background
This article takes a look at the Java virtual machine.
2. Knowledge
The Java Virtual Machine (JVM) is an abstract computing Machine. Like a real computing machine, it has an instruction set and manipulates various memory regions at run time.
Break down several processes to understand the big picture
The JVM keeps Java programs independent of the operating system (and hardware). The JVM does not rely on the Java programming language; it only knows about a specific binary format, the Class file format. A class file contains Java virtual machine instructions (or bytecode) and symbol tables, as well as other auxiliary information.
Break down several processes to understand:
- 1. Java files written in the Java language, such as xxx. Java files, are compiled into class file format “bytecode” files.
- 2. The bytecode file is compressed and packaged into a JAR file.
- Jar files can be distributed to Windows or Linux machines that have a JVM installed, and the class bytecode inside the Jar package is passed into the JVM. The JVM is responsible for interpreting the bytecode into specific machine instructions for execution.
The JVM can be defined by the JVM specification, and many vendors provide implementations of the JVM. For example, one of Oracle’s JVMS is called HotSpot, and another inherited from BEA Systems is JRockit. Android uses Dalvik VIRTUAL machine, ART virtual machine.
3. Core components of the JVM (three major items)
That is, the JVM has these components:
- 1. It has an instruction set (i.e., bytecode)
- 2. It has an instruction interpreter to parse instructions. (i.e., bytecode executor and JIT)
- 3. It can manipulate memory areas for loading and execution. (i.e., operating memory)
Focus on these three, and we’ll talk about them slowly.
3.1 bytecode instructions
The JVM has bytecode instruction specifications for the following task groups:
- Load and store
- The arithmetic
- Type conversion
- Object creation and manipulation
- Operand stack Management (Push/POP)
- Control transfer (branch)
- Method calls and returns
- An exception is thrown
- Monitor-based concurrency
These instructions are operational tasks that can be executed after being loaded into the JVM.
3.2 bytecode interpreter and just-in-time compiler
A bytecode interpreter is used to parse bytecode into a language that a computer can execute. Once a computer has a Java bytecode interpreter, it can run any Java bytecode program. The same Java program can run on a computer with the interpreter’s hardware architecture, making it “cross-platform.”
(2) Just-in-time Compiler (JIT) A JIT compiler can translate Java bytecode into local machine language at program execution time.
In general, Java bytecode execution through a bytecode interpreter is always slower than the execution of the same program compiled in a local machine language. The just-in-time compiler translates Java bytecode into local machine language during program execution, significantly speeding up the overall execution time.
3.3. Vm Architecture
The JVM operation memory has these:
-
The JVM has a heap for storing objects and arrays. This is where the garbage collector is going to work.
-
Code, constants, and other class data are stored in the Method Area.
-
Each JVM thread also has its own call stack (JVM stack) for storing “frames.” Each time the method is called, a new frame is created (put on the stack) and destroyed when the method exits.
-
Each frame provides an operand stack and an array of local variables. The operand stack is used to calculate operands and receive the “return value” of the called method, while local variable data is used to pass “method parameters.”
For compatibility. Each particular host operating system requires its own JVM and runtime implementation. Although the actual implementations may differ, they all semantically interpret bytecode in the same way.
It also has to do with an important garbage collection mechanism, which we’ll talk about later.
3.4 Class Loader
The basic unit of JVM bytecode is the class. The Class loader is used to identify and load content that conforms to the Java Class file format.
The class loader performs the following three activities in order:
- 1) Loading: searching and importing binary data content
- 2) Linking: Perform the following three substeps
Memory is allocated for class variables and initialized to the default value. Resolution: Convert symbolic references to direct references.
- 3) Initialization: Call Java code to initialize a class variable to its correct starting value.
In general, there are two types of class loaders:
- 1. Boot the classloader
- User-defined class loaders.
Each Java virtual machine implementation must have a Bootstrap Class loader capable of loading trusted classes, as well as an extension or application Class loader.
3.5 JVM languages
There are many JVM languages to choose from, such as Groovy, Scala, and Kotlin, all of which write code that can be compiled into bytecode and run on the JVM.
4. Garbage Collector (GC)
.
5. Reference:
Oracle’s Java virtual machine specification docs.oracle.com/javase/spec… Wikipedia en.m.wikipedia.org/wiki/Java_v…