Java stack and elf file stack

jvm runtime memory

The runtime memory of jvm is mainly divided into: heap, virtual machine stack, local method stack, method area (metadata area or persistent generation) , heap memory is divided into old generation and new generation.
Insert picture description here

The general execution process of the elf file

The elf file is a file generated by the C language system after being compiled and linked. It can be run on Linux and Unix operating systems. Its structure mainly adopts a segmentation method, which is divided into .bss, .text, .data and other segments. When the code segment is executed on the cpu, it will apply for a stack address space to execute the file. The specific process is as follows: The
stack is determined by the address value stored in the register at the top of the stack and the address value stored in the register at the bottom of the stack, and it is a continuous segment. In the address space , the cpu has a special register set that can fetch the instruction address, which is simplified as the instruction address register in this article. First, the cpu initializes a stack (the value of the stack top register and the stack bottom register are the same, and the stack length is 0), by The instruction address register reads the instruction (the code posted directly in this article, which is actually machine code), and hands the instruction to the cpu for processing. The cpu saves the instruction a=1 in the stack, and the register at the bottom of the stack adds a unit length, and the instruction register is also Auto-increment a length unit.
Insert picture description here
After that, the cpu reads b=1 and stores it in the stack, and the stack bottom register and instruction address register are auto-incremented.
Insert picture description here
When calling the d() function ( procedure call ), first save the information in each register (Put into the stack), and then push the stack according to the formal parameters of the procedure call, where a and b are the physical addresses of the previous a and b or the direct value (specifically different depending on the version), and the order of a and b is The order of entering the parameters is reversed. Stack top register, stack top register, instruction register value changes (all become 2 in the figure), this time is a new stack ( stack frame ), and a new stack is started in the new stack frame Calculate, a+b=3.
Insert picture description here
When encountering the " } " in the d method , the stack frame is over, and the stack starts at this time, and the saved value will be restored to the previous scene when it is popped out of the stack.
Insert picture description here
Finally, the main method is executed. , When encountering " } ", it is also popped, and the stack resource is recycled by the operating system, and the call is completed
Insert picture description here

java operand stack

Java's operand stack is composed of stack frames one by one. The stack frame also contains the local variable table, operand stack, return address and dynamic link . From here it is not difficult to see when the java operand stack and elf file are executed The overlap of some concepts of, such as the concept of stack frame, return address, etc.
Insert picture description here
Supplement: analyze the picture above, first of all, the first is the virtual address (hexadecimal), the interval is 8 (the mantissa is not 8 or 0), the first The second line is the specific value (local variable table information, stack frame information, etc.), all three lines are comments, which code is used for (also like stack frame) The
above picture is a section of jhsdb, which may be more abstract. However, it can be seen that the process of the procedure call is roughly the same as that of elf. Each method is a stack frame. Each time a method is called, the stack frame is pushed onto the stack.
Java also optimizes the local variable area, which is compared to the author's previous In the elf file, the formal parameters of the new method do not need to be stacked repeatedly.Java directly puts the local variable area at the bottom of a method, which is shared with the new stack frame, which optimizes space and time.
Insert picture description here

postscript

First of all, the operation of the elf file is summarized by the author after listening to the computer system composition of Nantah University. It may be different from the current operation of the elf file (only university teaching, and the course is relatively long and the author may understand it. There is a problem), the author also wants to specialize in java. If these understandings are wrong, I hope the big guys can correct them and they must actively correct them.

Guess you like

Origin blog.csdn.net/weixin_41046569/article/details/109593599