java—memory analysis

In JavaSE, memory management is handled by the Java Virtual Machine (JVM). There are many different memory areas inside the JVM, including the heap (Heap), stack (Stack), method area (Method Area), program counter (Program Counter), and local method stack (Native Method Stack).

Here are some examples of common memory profiling:

1. Create an object

When we use the new keyword to create an object, the JVM will allocate memory space for the object in the heap (Heap). For example:

MyObject myObj = new MyObject();

2. Variable assignment

When we assign a variable to another variable, JVM will allocate memory space for the new variable in the stack (Stack), and copy the value of the original variable to the new variable. For example:

int i = 10;
int j = i;

2. Method call

When we call a method, the JVM will create a new frame for the method in the stack (Stack), and save the parameters, local variables and other information of the method into the frame. For example:

public void myMethod(int x, int y) {
    
    
    int z = x + y;
}

// 调用方法
myMethod(10, 20);

3. Class loading

When the Java virtual machine loads a class, the JVM will create a Class object for the class in the method area (Method Area), and save the information of the class into the object. For example:

Class myClass = MyClass.class;

Above are some common examples of memory analysis in Java SE. Through the analysis of the memory management of the Java virtual machine, we can better understand the inner working principle of the Java program, so as to better develop and debug the program.

Guess you like

Origin blog.csdn.net/l_010/article/details/131234337