In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about how to understand the changes in the memory model during the implementation of Java. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
Main work: try to track the entire process executed by Java in chronological order and show the corresponding changes to the memory model in JVM.
Main purpose: hope to improve the understanding of how programming languages work through the tip of the iceberg of Java execution.
Take the following code as an example to track its execution:
Public class Car {
Private int speed
Public void setSpeed (int speed) {
This.speed = speed
}
Public void getSpeed () {
System.out.println (speed)
}
Public static void main (String [] args) {
Car car = new Car ()
Car.setSpeed (3)
Car.getSpeed ()
}
}
II. Implementation process
Next is the specific execution process, which consists of five steps: compiling, loading, executing main methods, executing member methods, and method return.
Step1: compiling
First of all, after we have completed the above source code, in order to get the program running, we need to compile it into a bytecode file. Bytecode is a cross-platform JVM machine language, which can be parsed by JVM, regardless of the underlying operating system.
Step2: loadin
When the code needs to be called, JVM loads the target bytecode into the method area and converts it into the run-time data structure of the method area, where the loading process is done through the class loader. Then a java.lang.Class object representing the class is generated in memory (not necessarily a heap) as an access entry to the various data structures of the class in the method area.
Step3: execute the main method
The main method can be called through the java.lang.Class object, as shown in the following code:
Method method = targetClass.getDeclareMethod ("main", String [] .class)
Method.invoke (null, (Object) new String [0])
After that, the PC register will point to the address of the main function in the method area, and the corresponding stack frame will be generated in the thread stack, which is mainly used to store the local variable table of the current method, the operation stack, and the method return address. Next, the PC register is offset to the backward address, and the execution engine begins to execute the main method body. When the statement Car car = new Car () is executed, the corresponding changes in the stack frame and heap are as follows:
Step4: executes member methods
The setSpeed method call procedure of the object car is similar to main. By indexing the member method address of car, the PC register will point to the setSpeed function address in the method area, and a new stack frame will be generated in the thread stack, in which the method return address is used to save the original PC address offset. When the assignment statement this.speed = speed completes, the corresponding changes in the stack frame and heap are as follows:
Step5: method returns
With the end of the execution of the setSpeed method, the corresponding stack frame in Stack is off the stack, and the pointer at the top of the stack points to the main stack frame again. At the same time, the PC register will be restored according to the method return address, thus continuing to execute the main method body. When the main method is also executed and off the stack, the main thread and the virtual machine instance die, and the program ends.
Virtual machine or a certain program language, as a low-level implementation, can meet most of the needs of upper-level users, but the requirements keep pace with the times, and one day users need to write their own underlying implementations, such as components, frameworks, and a new language. At this point, you need to open the original specification, destroy it, and then rebuild it, so as to define your own specification. This may be one of the reasons why we need to explore the bottom.
After reading the above, do you have any further understanding of how to understand the changes in the memory model during Java execution? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.