当前位置:网站首页>Introduction to JVM principle
Introduction to JVM principle
2022-07-03 04:33:00 【xmh-sxh-1314】
JVM Has always been a java An important part of advanced knowledge , If you want to java Further research in the field , be JVM It's a topic that can't be avoided , This series tries to make it easy to read , Explain JVM Necessary knowledge .
Operation process
We all know java The slogan that has been publicized is : A compilation , Run anywhere . So how does it come true ? Let's look at the picture below :
image
java After a compilation of the program , take java Code compiled as bytecode is class file , And then rely on different... On different operating systems java Virtual machine , Finally, it is converted to machine code of different platforms , It's finally implemented . In this way, can we deduce , If you want to in mac Running on the system , Do you just need to install mac java Just a virtual machine . So after understanding the basic principle , We try to do deeper research , An ordinary java What is the execution flow of the program ? For example, we wrote a piece of code like this :
public class HelloWorld { public static void main(String[] args) { System.out.print("Hello world"); } }
This program from compile to run , Finally print out “Hello world” What steps have been taken ? Let's go straight to the top :
image
java After the code is compiled, a bytecode file is generated (class file ), adopt :java HelloWorld perform , here java Find... According to the system version jvm.cfg, You can search your computer jvm.cfg Where is the file , It will be placed in different places according to your system version , For example, my file is in :C:\Program Files\Java\jdk1.8.0_101\jre\lib\amd64\jvm.cfg, Open it and have a look :
S{6G7J8WOT3$)72FL1DW5U7
This is the file on my computer , among -server KNOWN It means the name is server Of jvm You can use . If you search your computer at this time jvm.dll, You will find that it must be somewhere in your server Under the table of contents , Such as my :C:\Program Files\Java\jdk1.8.0_101\jre\bin\server\jvm.dll. In short, through jvm.cfg File found corresponding jvm.dll,jvm.dll It is java The main implementation of virtual machine . Next, initialize JVM, And get JNI Interface , What is? JNI Interface , Namely java Local interface , You want to. java It's compiled into class file ,JVM How to find this file from the hard disk and load it into JVM Li , It is through JNI Interface ( It is also often used for java And the operating system 、 Hardware interaction ), find class File and load it into JVM, And then find main Method , Finally, execute .
JVM The basic structure
Maybe through the description above , Everyone to JVM There is a rough understanding of the operation process , that JVM How exactly does the interior execute a class What about the documents , This is the last step in the picture above 6 What are the internal details of this step ? To understand the problem , First we have to take a look at JVM The internal structure of :
image
It is not difficult to see from this structure ,class The file is jvm After loading , after jvm Memory space allocation based on , It's ultimately done by the execution engine class Execution of documents . Of course, this process is assisted by other role modules , These modules work together to make a java The program runs successfully , These templates are described in detail below , They are also learning later jvm The most important part .
Memory space :
JVM Memory space contains : Method area 、java Pile up 、java Stack 、 Native Method Stack .
Method area is the area shared by each thread , Storage information 、 Constant 、 Static variables .
java The heap is also an area shared by threads , The instance of our class is in this area , You can imagine that one of your systems will produce many instances , therefore java The heap also has the largest space . If java There's not enough heap space , The program will throw OutOfMemoryError abnormal .
java The stack is a private area for each thread , It has the same life cycle as a thread , One thread corresponds to one java Stack , Every time a method is executed, an element is pushed into the stack , This element is called “ Stack frame ”, The stack frame contains the local variables in the method 、 An operation stack for storing intermediate state values , There's a lot of detail in it , We'll talk about it later . If java There's not enough stack space , The program will throw StackOverflowError abnormal , Think about the circumstances under which this error can easily occur , Yes , recursive , If recursion is deep , A lot of methods will be executed , The more ways java The more space the stack takes up .
Each frame represents a method ,Java Method can be returned in two ways ,return And throw exceptions , Both methods will cause the corresponding frames of the method to stack and free memory .
③ How the stack works
The data in the stack is in stack frame (Stack Frame) The format of exists , Stack frame is a memory block , Is a dataset , It's a dataset of method and runtime data , When a method A When called, a stack frame is generated F1, And is pushed into the stack ,A The method is called again B Method , So the stack frame F2 Also pushed into the stack ,B The method is called again C Method , So the stack frame F3 Also pushed into the stack …… After the sequence is completed , First pop, then go in ......F3 Stack frame , Then pop up F2 Stack frame , Then pop up F1 Stack frame .
follow “ First in, then out ”/“ Last in, first out ” principle .
Frame composition : Local variable region ( Including method parameters and local variables , about instance Method , And first save this type , The method parameters are placed strictly according to the declaration order , Local variables can be placed arbitrarily ), The stack of operands , Frame data area ( Used to help support constant pool parsing , Normal method return and exception handling ).
Local method stack roles and java Stack like , It's just that it's meant to execute local methods , The method stored in the local method stack calls the local method interface , Finally call the local method library , Implementation and operating system 、 The purpose of hardware interaction .
PC register , At this point our class has been loaded , Instance object 、 Method 、 Static variables go where they have changed , So here comes the question , How to execute the program , Which method is the first to execute , Which method is executed after , The order in which these instructions are executed is PC The register is in the tube , Its function is to control the execution order of program instructions .
Of course, the execution engine is based on PC The order of instruction in register allocation , Execute the program instructions one by one .
边栏推荐
- When using the benchmarksql tool to test the concurrency of kingbasees, there are sub threads that are not closed in time after the main process is killed successfully
- Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.
- The simple problem of leetcode: dismantling bombs
- vulnhub HA: Natraj
- [Chongqing Guangdong education] reference materials for design and a better life of Zhongyuan Institute of science and technology
- redis 持久化原理
- Prefix and (continuously updated)
- 2022 P cylinder filling test content and P cylinder filling simulation test questions
- 怎么用Kotlin去提高生产力:Kotlin Tips
- 关于开学的准备与专业认知
猜你喜欢
[Thesis Writing] how to write the overall design of JSP tourism network
The programmer went to bed at 12 o'clock in the middle of the night, and the leader angrily scolded: go to bed so early, you are very good at keeping fit
Contents of welder (primary) examination and welder (primary) examination in 2022
Arthas watch grabs a field / attribute of the input parameter
Joint set search: merge intervals and ask whether two numbers are in the same set
Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected
Leetcode simple question: check whether the string is an array prefix
[free completion] development of course guidance platform (source code +lunwen)
FFMpeg filter
Use the benchmarksql tool to perform a data prompt on kingbases. The jdbc driver cannot be found
随机推荐
Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.
[PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
Solve BP Chinese garbled code
[set theory] binary relation (example of binary relation on a | binary relation on a)
Leetcode simple question: check whether the array is sorted and rotated
[set theory] Cartesian product (concept of Cartesian product | examples of Cartesian product | properties of Cartesian product | non commutativity | non associativity | distribution law | ordered pair
Learning practice: comprehensive application of cycle and branch structure (I)
Redraw and reflow
关于开学的准备与专业认知
How to retrieve the password for opening word files
[literature reading] sparse in deep learning: practicing and growth for effective information and training in NN
Number of 1 in binary (simple difficulty)
金仓数据库KingbaseES 插件kdb_date_function
[set theory] inclusion exclusion principle (including examples of exclusion principle)
Two drawing interfaces - 1 Matlab style interface
怎么用Kotlin去提高生产力:Kotlin Tips
I've been in software testing for 8 years and worked as a test leader for 3 years. I can also be a programmer if I'm not a professional
Arthas watch grabs a field / attribute of the input parameter
Youdao cloud notes
Library management system based on SSM