Stack stack frame difference

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right


1. Method execution process analysis, the relationship between memory, stack, and stack frame

If the method is not called, the code inside the method will not be executed at this time; when the method is called, it will jump to the method to execute sequentially; when the method execution ends, it will return to the method call location to continue execution .

When the program is running, there will be a special memory area to save the calling relationship between methods. This area is called " stack " (here "stack" refers to a specific memory in JVM Area) When
a method is called, the relevant information of this method is stored in the stack (stacked).
After the method is executed, the corresponding information will be deleted from the stack (popped)
. The element is called a frame

Insert picture description here

Second, the difference between stack and heap

Java divides memory into two types: one is stack memory and the other is heap memory. Some basic types of variables defined in functions and object reference variables are allocated in the stack memory of the function. When a variable is defined in a code block, Java allocates memory space for this variable in the stack. When the variable exceeds the variable After the scope of the variable, Java will automatically release the memory space allocated for the variable, and the memory space can be used for other purposes immediately.
Some basic types of variables and object reference variables defined in the function are allocated in the stack memory of the function. When a variable is defined in a code block, Java allocates memory space for this variable in the stack to store the calling relationship between methods.

Heap memory is used to store objects and arrays created by new. The memory allocated in the heap is managed by the automatic garbage collector of the Java virtual machine. After an array or object is generated in the heap, you can also define a special variable in the stack so that the value of this variable in the stack is equal to the first address of the array or object in the heap memory, and the variable in the stack becomes After the reference variable of the array or object, the reference variable in the stack can be used in the program to access the array or object in the heap. The reference variable is equivalent to a name for the array or object. Reference variables are ordinary variables. They are allocated on the stack when they are defined. Reference variables are released after the program runs outside of its scope. The array and the object themselves are allocated in the heap, even if the program runs outside the code block where the statement that uses new to generate the array or object is located, the memory occupied by the array and the object itself will not be released, and the array and the object are not referenced to it. When it becomes garbage, it cannot be used, but it still occupies the memory space, and is collected (released) by the garbage collector at an indefinite time.

This is also the reason why Java occupies more memory. In fact, variables in the stack point to variables in heap memory, which are pointers (references) in Java.

The reference variable is equivalent to a name for the array or object, and the reference variable in the stack can be used in the program to access the array or object in the heap in the future.

Three, the allocation of variables in memory in java

1. Class variable (static modified variable): When the program is loaded, the system opens up memory for it in the heap, and the memory address in the heap is stored in the stack for high-speed access. The life cycle of static variables-continues until the entire "system" is shut down

2. Instance variables: When you use the java keyword new, the system opens up a continuous space in the heap and allocates it to variables (for example, class instances), and then converts it through a hash algorithm according to the scattered heap memory addresses It is a long series of numbers to represent the "physical location" of this variable in the heap. The life cycle of instance variables-when the references of instance variables are lost, they will be included in the recyclable "list" by the GC (garbage collector), but the heap memory is not released immediately

3. Local variables: Local variables are declared in a method or a certain code segment (such as a for loop). When it is executed, memory is opened in the stack. Once the local variable is out of scope, the memory is released immediately

Guess you like

Origin blog.csdn.net/weixin_45070922/article/details/112790484