What is the JVM

2. What are the parts of the JVM

3. Program counters

4. Vm stack

5. Local method stack

6. The heap

7. Method of area

1. What is the JVM in our daily JAVA development process, from the original at the beginning of the use of Servlet, JSP, and then to the SSM, SpringBoot, and queue, registry and so on. These are the so-called JAVA upper level technologies, and that’s where people are focusing their attention and learning. But little is known about JAVA’s technical core, the JAVA Virtual Machine. So most people think that the top skills are the most important and the basic knowledge is not important, which is a “put the cart before the horse mentality.” If we think of the API as a mathematical formula, then the knowledge of the JAVA Virtual Machine is like the derivation of the formula. A virtual machine (JVM) is a virtual machine that runs JAVA bytecode, which translates JAVA bytecode files into files that the operating system can execute. So where does JAVA bytecode come from? Is compiled by the JAVAC compiler (JAVAC is a compiler whose job is to convert the Java source language into a language recognized by the JVM), so the entire compilation and execution process is as follows:

2. What are the parts of the JVM

Let’s start with a picture:

The JVM byClass loading subsystem, runtime data area, execution engine and native method interface, let’s first explain their overall workflow, and then analyze each part:

The JAVA code that we write is loaded first by the class loading subsystem into the runtime data area, so that while JAVA is running, it can retrieve information about classes, variables, and so on from memory, and the runtime data area is the area that we’re going to focus on and so on.

However, after the bytecode is loaded into the JVM, the bytecode does not run on the operating system, so we need to execute the engine, which interprets/compiles the bytecode instructions to local machine instructions on the corresponding platform. Simply put, the execution engine in the JVM acts as a translator for translating high-level languages into machine languages.

The purpose of the native method interface is to fuse different programming languages for Java. It was originally intended to fuse C/C++ programs.

Today we’ll focus on what makes up the runtime data area and what each part does.

Every thread has a program counter, which is thread private. Its purpose is to store where the code is currently running. This is called a line number indicator of the bytecode being executed by the current thread. The basic functions used to complete loops, jumps, exception handling, etc., are areas where there are no OutOfMemoryError cases specified in the JAVA virtual Machine specification.

4. Virtual machine virtual machine stack in the exclusive area thread stack competent JAVA program is running, is the time to create the thread creation, its life cycle is to follow the thread of the life cycle, the end of the thread stack memory is released, for the stack, there is no garbage collection problem, as long as the end of a thread to stack Over, life cycle and thread is consistent, Is thread private, the 8 basic type variables + object reference variables + instance methods are allocated in the stack memory of the function.

Here’s how the stack works:

First, the stack has two operations, the stack out and the stack push. Whenever we run a method, we push the stack, and when the method is finished, we exit the stack.

Let’s use code as an example:

 public static void main(String[] args) {

    public static void method01(){

    public static void method02(){

After the main method executes, the main() method has a push operation:

Then run method01 and method02 again:

Method01 and method02 perform sequential pushes.


After executing method02, the stack is exited

After executing method01, the stack is exited

After main is executed, the stack is exited

Each execution of a method generates a stack frame, which is saved to the top of the stack. The top stack is the current method, and when the method finishes executing, the stack frame is automatically removed from the stack.

Each stack frame is used to store information about local variable tables, operand stacks, dynamic links, method exits, etc.

The local method stack is in the thread exclusive zone. The function of the local method stack is very similar to that of the virtual machine stack, except that the virtual machine stack performs non-virtual Java method (that is, bytecode) services, while the local method stack performs Native method services for the virtual machine. The language, usage mode, and data structure of methods in the native method stack are not mandated in the VIRTUAL machine specification, so specific virtual machines are free to implement it.

6. The heap

A JVM instance only has one heap memory, which is the main working area of the garbage collector. The size of the heap memory is adjustable. After the class loader reads the class file, it needs to put the classes, methods, and constants in the heap memory to save the real information of all reference types for the executor to execute

7. Method area The method area is in the thread shared area to store the runtime constant pool, class information that has been loaded by the virtual machine, constants, static variables, real-time compiled code and other data generally do not choose to recycle the garbage collection. This area contains the method area is always unique in the entire program elements, such as class, static variables.