Simple Analysis of Java program memory and java program analysis
This article will briefly describe the memory allocation of the JVM (Java Virtual Machine) when we run the Java program.
First, let's take a look at the following terms:
1. Stack. In general, the basic data type is directly allocated space in the stack, and local variables (variables defined in the method code segment) are also directly allocated space in the stack, after the local variable method is executed, the space is immediately reclaimed by JVM. Another type is reference data, that is to say, the reference corresponding to the object created with the keyword new is also in the stack space. At this time, JVM allocates an address space (equivalent to a house number, which can be used to find your home) to the object reference in the stack space, and allocates a space to the referenced object in the heap space, the address reference in the stack space points to the object area in the heap space (find the address through the house number );
2. heap is generally used to store data with the new keyword.
Next, we will use a program to intuitively view the JVM memory when the program is running,
1 package demo; 2 3 public class Student { 4 private String name; 5 private int age; 6 7 public void study() { 8 System.out.println("I love study!"); 9 }10 11 public String getName() {12 return name;13 }14 15 public void setName(String name) {16 this.name = name;17 }18 19 public int getAge() {20 return age;21 }22 23 public void setAge(int age) {24 this.age = age;25 }26 }
1 package demo; 2 3 public class StudentDemo { 4 public static void main(String[] args) { 5 Student student = new Student(); 6 7 System.out.println(student.getName() + ":" + student.getAge()); 8 student.setName("John"); 9 student.setAge(23);10 System.out.println(student.getName() + ":" + student.getAge());11 12 student.study();13 14 Student student2 = student;15 student2.setName("Jack");16 student2.setAge(25);17 System.out.println(student2.getName() + ":" + student2.getAge());18 System.out.println(student.getName() + ":" + student.getAge());19 }20 }
When we run the program, JVM will compile the Student class and StudentDemo class and load it to a place in the JVM called the method area, the member variables and member methods of the class are also loaded into the method area. The memory model is as follows:
We can see that each of the study methods has a hexadecimal mark on the right, but the name and age variables do not exist, because each object has its own member variables, the member methods in the class can be shared by every object. To save memory space, JVM assigns this mark (also called the memory address) to the method to facilitate search and call of each new object, then the JVM will automatically find the main method and apply for a space for the main method in the stack. This process is also called the stack, and then runs the 5th lines of code in our Student class. At this time, JVM allocates a piece of memory to the Student object in the heap space and assigns a memory address to it (if the object's member variables are not assigned a value, JVM assigns an initial value to the variable ), allocate a memory space in the stack to point to the memory address of the Student object area in the heap space. The memory model is as follows:
Next, let's look at lines 8th and 9th of the Code. The program assigns values to the member variables of the student object. JVM will find the student class variables in the heap memory according to the address pointed by Student, and new value for the variable
Row 3: The student object calls the study method. JVM applies for a memory space for the study method in the stack space.
After the study method is executed, the stack space is immediately released. Line 3 of the Code points to the address pointed to by student.
Lines 15 and 16 of the Code assign values to the student2 variable. Because student2 and student point to the same place, the value of the variable in the student object is also changed.
By now, all the code in the main method is executed, and the stack space occupied by the main method is recycled, while the heap space is waiting for GC to be recycled.
The code execution result is as follows:
null:0John:23I love study!Jack:25Jack:25
It is relatively simple to write. It is only used as a knowledge record. please correct me!