当前位置:网站首页>Explain the JVM clearly at one time and don't be asked by the interviewer again
Explain the JVM clearly at one time and don't be asked by the interviewer again
2022-06-26 02:25:00 【Love dad】
JVM It has always been a must for an interview , Everybody knows , But it doesn't seem particularly easy to figure it out .JVM My knowledge points are too scattered , No system , So it's not easy to summarize , Let's help you solve this problem today , Write an article about JVM Explain the structure of .
JVM Can be divided into 5 Parts of , Namely :
1、 Class loader (Class Loader)
2、 Run time data area (Runtime Data Area)
3、 Execution engine (Execution Engine)
4、 Local library interface (Native Interface)
5、 Local method library (Native Libraies)
The most complex of these is the runtime data area , It can also be divided into method area 、 Virtual machine stack 、 Native Method Stack 、 Pile up 、 Program counter , And the method area and heap are shared by threads , Virtual machine stack 、 Native Method Stack 、 Program counters are thread isolated ,JVM The structure of is shown in the figure below .
Make it clear. JVM The structure of the virtual machine , Next we will explain each part of it in detail .
Class loader : Load bytecode file to memory .
Execution engine : Yes JVM Instruction parsing , Translate into machine code , Submit to the operating system after parsing .
Local library interface : for Java Call a native library that integrates different development languages .
Local method library :Java Implementation of local method .
Run time data area :JVM Core memory space structure model .
The runtime data area is JVM The most important part of memory structure , Next, we will explain in detail the various components of the runtime data area .
1、 Method area
The method area stores the class information loaded by the virtual machine 、 Constant 、 Static variables , Real time compiler compiled code and other data . The method area is a norm , Permanent generation is an implementation of the method area , Here is a frequently asked interview question :JDK 7 In previous versions, the string constant pool was placed in the permanent generation ,JDK 7 Moved the string constant pool to the heap ,JDK 8 Deleted the permanent generation directly , Use meta space instead of permanent space .
2、 Native Method Stack
Local method stack and Java The function and principle of stack are basically the same , Can be used to execute methods , The difference is Java The stack executes Java Method , The local method stack executes local methods .
What is? Java Local methods for ?Java It is a high-level programming language based on application layer , Unable to access the underlying information of the operating system , Such as underlying hardware devices , At this time, you need to use other languages to complete the functions , such as C Language , The principle of using the local method is as follows :
1、 stay Java Statement in the program native The method of decoration , Only method definition , There is no way to achieve , Will be Java Compile the file into a bytecode file .
2、 use javah Compile bytecode file , Generate a .h file .
3、 Write a .cpp File implementation .h Method in file .
4、 take .cpp Compile files into DLL files .dll .
5、 Use System.loadLibrary() Load dynamic link library file .
In this way, the local method can be called , use Java Call not Java Write the interface , The basic principle is to use the reflection mechanism , Find... While running .dll File and parse , Create objects and methods according to the file names in the dynamic link library , Then we can use the object to call the method .
Common local methods are :public final native Class<?> getClass()、public native int hashCode()、protected native Object clone().
3、 Program counter
Program counters take up less memory space , Is the bytecode line number indicator executed by the current thread , Select the next bytecode instruction to execute by changing the value of this counter . The program counters between multiple threads are independent of each other , They don't influence each other , To ensure that each thread can find the specific execution location after recovery .
4、Java Pile up
Java Heap is used to store instantiated objects ,Java Heap shared by all threads , Created when the virtual machine starts , Used to store object instances , yes Java Big head in memory structure , Take up most of the space , yes GC The main management area of , It can also be divided into the younger generation 、 Old age 、 Forever ,JDK 8 And later removed the permanent generation .
The younger generation
The younger generation can be divided into Eden,from Survivor,to Survivor.
Eden District : When the object was first created , Store in Eden District , If Eden There's no room for , Put it in Survivor District , Even in the old generation .
Survivor District :Survivor It can also be divided into Survivor From and Survivor To,GC Use when recycling , take Eden Objects that survive in are stored in Survior From in , The next time you recycle , take Survior From Objects in are stored in Survior To in , eliminate Survior From , Repeat step for the next recovery ,Survior From become Survior To,Survivor To become Survivor From, In turn, cycle , At the same time, each time it is recycled , The age of the subject is +1, An object whose age has increased to a certain extent , Move to the older generation .
Old age
Store long-lived objects .JDK 8 Then use meta space instead of permanent space .
Meta space
Java 8 Then start putting the metadata of the class in the heap memory , This region is called a meta space , stay Java 7 And before , A meta space is placed in a permanent generation ,Java 8 Then it was separated .
Metaspace and permutation are implementations of the method area , The method area is just a specification , stay Java 7 after , The string constant pool originally located in the permanent generation of the method area has been moved to Java In the pile , Because the memory space of the permanent generation is extremely limited , If called frequently inter Method , Memory cannot store so much data . stay Java 8 Then the permanent generation is completely deleted , Use meta space instead of permanent generation .
Metaspace uses local memory , For permanent use JVM Memory , So the advantage of using meta space is that the memory of the program is no longer limited to JVM Memory , How much space is left in local memory , Meta space can be as big as , Solved the problem of insufficient space .
5、 Virtual machine stack
Java Memory model for method execution ,Java Multiple stack frames are stored in the stack , Each stack frame corresponds to a called method , It mainly includes local variable table 、 The stack of operands 、 Dynamic links 、 Method return address ( Methods the export ). The execution of each method ,JVM All create a stack frame , And press the stack frame into Java Stack , Method execution completed , The stack frame is out of the stack .
Local variable table : Store all variables during method execution , Include local variables and formal parameters declared in the method .
The stack of operands : The calculation process in the method is completed with the help of operand stack , Push the data participating in the calculation into the operand stack ,
The specific operation mode of stack is as follows , The compiler is implemented through two stacks , One is a stack that holds operands , The other is to store the stack of operators . We traverse the expression from left to right , When it comes to Numbers , Push directly into the operand stack . When an operator is encountered , First, compare with the top element of the operator stack , If it is higher than the priority of the current stack top element , Press in directly , Otherwise, take the operator at the top of the current stack , At the same time, the first two data of the operand stack are fetched for operation , And push the result into the operand stack . Repeat the above steps again , Until the current operator is pushed onto the stack , When there is no new operator to stack , Get the current top element and the two operations of the operand stack , Carry out operations , Push the result onto the stack of operands , If the method definition needs to return a value , Simply return the top element of the operand stack .
Method return address : After a method call is completed, return to the place where it was called , So keep the address that can be returned to the method call in the stack frame .
Each thread has its own Java Stack , Are independent of each other , Various methods can be executed simultaneously , The execution of each method is a stack in and stack out process of stack frame ,Java The virtual machine stack is used to store stack frames , After the method call , The frame will be destroyed .
That's all JVM Memory structure , Have you learned everything ?
边栏推荐
猜你喜欢
win32
[defect detection] automatic defect detection of printed circuit board based on Matlab GUI [including Matlab source code 1912]
OA process editing
Markov decision process (MDP): Blackjack problem (mc-es)
In depth good article: what is supernetting?
深度好文:什么是超网 Supernetting?
vs2015+PCL1.8.1+qt5.12-----(1)
SQL column value to row value (unpivot)
第一章:渗透测试的本质信息收集
Prompt to update to the latest debug version during vscode debugging
随机推荐
Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) D. Felicity‘s Big Secret Revealed
Pie chart metamorphosis record, the liver has 3000 words, collection is to learn!
Cow sequencing problem
Shell learning record (III)
Snake game
One year's work
图的广度优先遍历
Redis-SDS
Consumer of microservices
树莓派 + AWS IoT Greengrass
WPF 窗口居中 & 变更触发机制
OA process editing
Ardiuno smart mosquito racket
vs2015+PCL1.8.1+qt5.12-----(1)
regular expression
Analytic hierarchy process
FPGA实现图像二值形态学滤波——腐蚀膨胀
二分查找
Connectez Le projecteur
Weishi camera display