JVM memory for beginners (heap, stack and method)

In the past two days, I have read the book Explaining the JVM in a simple way. It is recommended for advanced java programmers to read it. It
is of great help for you to understand the underlying and operating mechanism of JAVA.
I don't want to talk nonsense. Enter the topic:
first understand the specific concepts:
The memory of JAVA's JVM can be divided into 3 areas: heap, stack and method area

Heap area:
1. All objects are stored, and each object contains information of a corresponding class. (The purpose of class is to obtain operation instructions)
2. JVM has only one heap area (heap) shared by all threads. The heap does not store basic types and object references, but only the
stack area of ​​the object itself:
1. Each thread contains a stack area , only the basic data type objects and the reference of the custom object (not the object) are stored in the stack, and the objects are stored in the heap area
2. The data in each stack (primitive types and object references) are private, other stacks Can not access.
3. The stack is divided into three parts: the basic type variable area, the execution environment context, and the operation instruction area (storing operation instructions).
Method area:
1. Also called static area, like the heap, it is shared by all threads. The method area contains all class and static variables.
2. The method area contains elements that are always unique in the entire program, such as class and static variables.
In order to more clearly understand the shady happenings in the runtime data area, let's prepare 2 small props (2 very simple small programs).
AppMain.java

 public   class  AppMain                

//When running, jvm puts the information of appmain into the method area

{

public static void main(String[] args) //The main method itself is put into the method area.

{

Sample test1 = new Sample( " test 1 " ); //test1 is a reference, so put it in the stack area, Sample is a custom object that should be placed in the heap

Sample test2 = new  Sample( " 测试2 " );

test1.printName();

test2.printName();

}


Sample.java

 public class Sample //When running, jvm puts the information of appmain into the method area

{

/** Example name */

private name; //After the new Sample instance, the name reference is placed in the stack area, and the name object is placed in the heap

/** Construction method*/

public  Sample(String name)

{

this .name = name;

}

/** output */

public void printName() //The print method itself is placed in the method area.

{

System.out.println(name);

}


OK, let's start the action, the starting command is: "java AppMain", bring our action guide map in the bag, Let's GO!

The system receives the instruction we sent and starts a Java virtual machine process. This process first finds the AppMain.class file from the classpath, reads the binary data in this file, and then stores the class information of the Appmain class in the runtime data area. in the method area. This process is called the loading process of the AppMain class.
Next, the Java virtual machine locates the bytecode of the Main() method of the AppMain class in the method area and starts to execute its instructions. The first statement of the main() method is:
Sample test1=new Sample("Test 1"); The
statement is very simple, that is, let the java virtual machine create a Sample instance, and let the reference variable test1 refer to this instance. It seems like a small case, let's track the Java virtual machine and see how it performs this task:
1. When you look at the Java virtual machine, isn't it just to create a Sample instance, it's simple, so just go straight Go to the method area and find the type information of the Sample class first. As a result, hehe, I didn't find @@, there is no Sample class in the method area at this moment. But the Java virtual machine is not a stubborn idiot. Therefore, it promotes the style of "do it yourself and have enough food and clothing", immediately loads the Sample class, and stores the type information of the Sample class in the method area.
2. Okay, the information is found, let's start working. The first thing the Java virtual machine does is allocate memory in the heap area for a new Sample instance that holds a reference to the type information of the Sample class in the method area. The reference mentioned here actually refers to the memory address of the type information of the Sample class in the method area. In fact, it is somewhat similar to the pointer in the C language~~, and this address is stored in the Sample instance. in the data area.
3. In the JAVA virtual machine process, each thread will have a method call stack, which is used to track a series of method call processes in the thread running. Each element in the stack is called a stack frame. When a method is called, a new frame is pushed onto the method stack. The frame here is used to store method parameters, local variables and temporary data during operation. OK, the principle is over, let's continue our tracking action! Test1 before "=" is a variable defined in the main() method. It can be seen that it is a local variable. Therefore, it will be added to the JAVA method call stack of the main thread executing the main() method. And "=" will point this test1 variable to the Sample instance in the heap, that is, it holds a reference to the Sample instance.
OK, so far, the JAVA virtual machine has completed the execution of this simple statement. Referring to our action guide map, we finally figured out a little bit of the JAVA virtual machine, COOL!
Next, the JAVA virtual machine will continue to execute subsequent instructions, continue to create another Sample instance in the heap area, and then execute their printName() methods in turn. When the JAVA virtual machine executes the test1.printName() method, the JAVA virtual machine locates the Sample instance in the heap area according to the reference held by the local variable test1, and then locates the Sample class in the method according to the reference held by the Sample instance The type information of the printName() method is obtained to obtain the bytecode of the printName() method, and then the instructions contained in the printName() method are executed.

Guess you like

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