JVM area division

overview


For more blog content visit only love to eat dragon fruit , click to learn more


When the JVM runs the code, it uses multiple memory spaces, and different memory spaces are used to store different data, and then cooperate with the code flow to make the system run.

Store class loading information

To give the simplest example, for example, now we know that the JVM will load classes into the memory for subsequent operation, so there must be a memory area in the JVM to store the classes we write.
This area is called the method area in versions before JDK 1.8 , which represents an area in the JVM. It is mainly to put the classes loaded from the ".class" file, and there will be some things like constant pools in this area. But after JDK 1.8, the name of this area was changed to "Metaspace" , which can be considered as "metadata space". Of course, it mainly stores various types of related information written by ourselves.

public class Client {
    
    
    public static void main(String[] args) {
    
    
        //使用支付宝付款
        Shopping shop = new Shopping(new Alipay());
        shop.payMoney();
    }
}

public class Shopping
{
    
    
    private PayMethod payMethod;
    public Shopping(PayMethod payMethod)
    {
    
    
        this.payMethod = payMethod;
    }
    //付钱,支付用什么方式付钱,客户自己选择
    public void payMoney()
    {
    
    
        String name = payMethod.getClass().getName();
        System.out.println("我在购物,现在要付钱了");
        payMethod.payMoney();
    }
}

public class Alipay implements PayMethod
{
    
    
    @Override
    public void payMoney()
    {
    
    
        System.out.println("我在使用支付宝支付");
    }
}

insert image description here

![image.png](https://img-blog.csdnimg.cn/img_convert/2f72a7cd4a9be28a3a995c51a0cbdf5a.png#averageHue=#f7f7f7&crop=0&crop=0&crop=1&crop=1&height=377&id=PokIW&margin=[object Object.]&name=im png&originHeight=753&originWidth=961&originalType=binary&ratio=1&rotation=0&showTitle=false&size=48746&status=done&style=none&title=&width=480.5) When the code is running, it needs to execute one method in the code. When running the method, there are many variables in the
method The things of the class need to be placed in a certain memory area. If some objects are created in the code, these objects also need memory space to store.

program counter

The Java code we wrote will be translated into bytecode, corresponding to various bytecode instructions. First, the Java code is compiled into bytecode instructions, and then the bytecode instructions must be executed one by one, so that code execution can be realized. Effect. So when the JVM loads the class information into the memory, it will actually use its own bytecode execution engine to execute the code instructions compiled by the code.
insert image description here

When executing bytecode instructions, a special memory area is needed in the JVM, that is, the "program counter".
This program counter is used to record the location of the currently executing bytecode instructions , that is, to record the current execution which bytecode instruction.
JVM supports multiple threads, so in fact, the code may open multiple threads to execute different codes concurrently, so there will be multiple threads to execute different code instructions concurrently, so each thread will have its own program counter. It specifically records which bytecode instruction is currently executed by the current thread.
insert image description here

Java virtual machine stack

When Java code is executed, it must be a thread to execute the code in a certain method, even if it is the following code, there will be a main thread to execute the code in the main() method, and the main() method will be executed in the main thread When the code is instructed, the program counter corresponding to the main thread will record the position of the instruction executed by itself.

public class Client {
    
    
    public static void main(String[] args) {
    
    
        //使用支付宝付款
        Shopping shop = new Shopping(new Alipay());
        shop.payMoney();
    }
}

In the method, we often define some local variables in the method. For example, in the above main() method, there is actually a " " shoplocal variable, which refers to an Shoppinginstance object. Don't worry about this object. , first look at methods and local variables.
Therefore, the JVM must have an area to save data such as local variables in each method , and this area is the Java virtual machine stack. Each thread has its own Java virtual machine stack. For example, the main thread here will have its own Java virtual machine stack, which is used to store the local variables of the methods it executes. If a thread executes a method, a corresponding stack frame is created for the method call.
In the stack frame, there are local variable tables, operand stacks, dynamic links, method exits, etc. of this method. Focus on local variables first.
For example, if the main thread executes the main() method, then a stack frame will be created for the main() method, and the Java virtual machine stack of the main thread will be pushed into the stack frame of the main() method. At the same time, the corresponding " " will be stored in the stack frame of the main() shopmethod local variables. Then assume that the main thread continues to execute Shoppingthe method in the object, such as the following, payMoneya local variable is defined in the " " method: " name"
insert image description here

If the " payMoney" method is executed, the " payMoney" method will also be popped from the Java virtual machine stack.
The above is the role of the "Java Virtual Machine Stack" component in the JVM**: when any method is called and executed, a stack frame will be created for the method and then pushed onto the stack. **Data such as local variables corresponding to this method are stored in the stack frame, including other related information about the execution of this method, and the stack will be popped after the method is executed.
insert image description here

Java heap memory

Java heap memory, here is where the various objects we create in the code are stored.
For example the following code:

public class Client {
    
    
    public static void main(String[] args) {
    
    
        //使用支付宝付款
        Shopping shop = new Shopping(new Alipay());
        shop.payMoney();
    }
}

The above " new Shopping()" code is to create an Shoppingobject instance of a class, which will contain some data, as shown in the following code.

public class Shopping {
    
    
    private PayMethod payMethod;
    public Shopping(PayMethod payMethod) {
    
    
        this.payMethod = payMethod;
    }
    //付钱,支付用什么方式付钱,客户自己选择
    public void payMoney() {
    
    
        String name = payMethod.getClass().getName();
        System.out.println("我在购物,现在要付钱了");
        payMethod.payMoney();
    }
}

The " Shopping" in this " " class payMethodis a data belonging to this object instance. Object instances like Shoppingthis are stored in the Java heap memory.
Similar objects will be placed in the Java heap memory area Shopping, and then because we created Shoppingthe object in the main method, when the thread executes the code of the main method, it will be in the local variable table of the stack frame corresponding to the main method, let A reference type " shop" local variable to store Shoppingthe address of the object. It is equivalent to that you can think that the " " in the local variable table shoppoints to the object in the Java heap memory Shopping.
insert image description here

A full-process talk about the core memory area

insert image description here
io=1&rotation=0&showTitle=false&size=98630&status=done&style=none&title=&width=651.5)

  1. When the JVM process starts, it will first load the Client class into memory. Then there is a main thread, which starts to execute the main() method in Client.
  2. The main thread is associated with a program counter, so which line of instruction it executes will be recorded here. When the main thread executes the main() method, it will push a The stack frame of the main() method.
  3. Then you will find that you need to create an Shoppinginstance object of a class, and Shoppingthe class will be loaded into memory at this time.
  4. Then an Shoppingobject instance will be created and allocated in the Java heap memory, and a " " variable will be introduced into the local variable table in the stack frame of the main() method shopto refer to Shoppingthe address of the object in the Java heap memory.
  5. Then, the main thread starts to execute Shoppingthe method in the object, and will sequentially push the stack frame corresponding to the method it has executed into its own Java virtual machine stack, and then remove the stack frame corresponding to the method from the Java virtual machine stack after the method is executed the stack

Guess you like

Origin blog.csdn.net/u010859650/article/details/127975766