Dabai became the fourteenth day of Java software siege lion (method to perform JVM memory analysis)

Memory analysis during method execution

During the execution of the method, how is the memory allocated in the JVM and how does the memory change?

  • The method is only defined, not called, and will not be executed, and the method will not be allocated "run-owned" memory space in the JVM. Only when this method is called, the space to which this method belongs will be dynamically allocated .
  • There are three main memory spaces in the JVM memory division:
    1. Method area memory
    2. Heap memory
    3. Stack memory
  • About the stack data structure :
    1. Stack: stack is a data structure
    2. The data structure reflects the storage form of the data.
    3. Data structure is an independent subject and does not belong to the category of any programming language, but data structure is used in most programming languages.
    4. As a programmer, you need to be proficient in advance: data structure + algorithm [a language compulsory for computer majors]
    5. Common data structures: arrays, queues, stacks, linked lists, binary trees, hash tables...
  • Where are the method code snippets? Where is the memory allocated during the execution of the method?
    1. The method fragment is a part of the .class bytecode file, and the bytecode file is placed in the method area when the class is loaded. Therefore, the method area memory in the three main memory spaces in the JVM first contains data and stores code fragments.
    2. Although there is only one copy of the code fragment in the method area memory, it can be called repeatedly. Every time this method is called, an independent place of activity needs to be allocated to the method and allocated in the stack memory. [Space where the method is allocated in the stack memory]
  • When a method is called, an independent memory space is allocated to the method and allocated on the stack. At this time, a stack pushing action occurs. After the method execution ends, all the memory space allocated to the method is released, and the stack popping action occurs.
    Push stack: allocate memory to the method.
    Pop stack: release the memory space of this method.
  • Local variables are stored on the stack. Local variables are allocated on the stack during the runtime.

Insert picture description here
Stack memory:
1) Provides running space for java method
2) Once the method is called, the corresponding stack frame will be created in the stack, and the entire execution process of the method is the process from the stack frame corresponding to the method from the stack to the stack . In other words, the method is called and pushed into the stack (push into the stack), and the method execution ends and is popped out (pop the stack).
3) The stack is first-in-last-out, first-in-first-out (the method called first ends last, and the method called later ends first)
4) The variables in the stack belong to methods, so they are all local variables , and local variables must Initialization value .
5) The stack life cycle is consistent with the life cycle of the thread to which it belongs, and the stack can be considered to have an automatic destruction mechanism.

Heap memory:
1) It is to open up space for entity objects, in other words, the space for entity objects is opened up in the heap. Anything that is new is an object.
2) The variables in the heap are object variables, because they belong to the object, and are generated as the object is created and destroyed as the object is destroyed.
3) The variables (object variables) in the heap have default values:

  • Integer: 0
  • Floating point type: 0.0
  • Boolean: false
  • char type: ''
  • Object: null

4) The heap does not have an automatic destruction mechanism, and the garbage in it is collected by the garbage collector (the collection algorithm is used to determine which object is garbage and then cleaned up)
5) The space in the heap has a hexadecimal first address as the address to distinguish .

Method area: The
method area stores data such as class information, constants, static variables, and code compiled by the just-in-time compiler that have been loaded by the virtual machine.
Constants are stored in the runtime constant pool in the method area.

Important: When the method is called, when the parameters are passed, the "value" saved in the variable is actually passed.

Guess you like

Origin blog.csdn.net/qq2632246528/article/details/112843950