Java memory analysis

java memory analysis

Object memory analysis

The content is selected from Shang Silicon Valley.
Insert picture description here
As shown in the figure, first create an address in the stack space, which directly points to the variable in the heap space, and the heap space is the place where the variables are actually stored. The stack space only stores the address, and the variable names are different. Look at the JVM In the future, different stack spaces will be allocated to store two different variable names, but the two variable names store the same address and point to the same place in the heap space.

Memory analysis of object array

Insert picture description here
It can be seen that when an object array is created, the array name is in the stack space, but each array object is in the heap space. Although the address is still stored in the array, the difference from creating a single object this time is that the object of a single object The name is in the stack space, and the address pointing to the heap space is stored. The object name of the element in the object array, that is, stus(i), is also stored in the heap space, and the address pointing to the remaining heap space is also stored.

JVM memory structure

Insert picture description here
The figure shows the memory structure of the JVM. After the java source code is compiled (javac), a bytecode file with the class suffix will be generated. The .class file is generally stored in the bin folder, and the source is in the code as in the src folder to determine a certain variable Whether on the heap or on the stack, this step is determined at runtime (java) rather than at compile time. After compilation is completed, it is only a file in the hard disk, and the class corresponding to the bytecode file is loaded to In the memory, this is the allocation of memory space.

The JVM memory structure has extreme points that need to be paid attention to
1. The virtual machine stack is the stack structure that is usually mentioned; local variables are generally stored in the stack structure.
2. The heap stores our new structures, such as arrays, objects, and even objects Array.
ps: The properties of the object are loaded into the heap space
3. The method area stores the methods of the class.

Guess you like

Origin blog.csdn.net/Meloneating/article/details/112971855