当前位置:网站首页>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 .
边栏推荐
- Two drawing interfaces - 1 Matlab style interface
- 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
- 2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank
- [BMZCTF-pwn] 18-RCTF-2017-Recho
- MongoDB 慢查询语句优化分析策略
- GFS distributed file system (it's nice to meet it alone)
- Smart contract security audit company selection analysis and audit report resources download - domestic article
- [dynamic programming] subsequence problem
- Leetcode simple question: check whether the string is an array prefix
- Reptile exercise 02
猜你喜欢

Auman Galaxy new year of the tiger appreciation meeting was held in Beijing - won the double certification of "intelligent safety" and "efficient performance" of China Automotive Research Institute

Number of 1 in binary (simple difficulty)

JVM原理简介

2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination

Learning practice: comprehensive application of cycle and branch structure (I)

Data Lake three swordsmen -- comparative analysis of delta, Hudi and iceberg

Number of uniform strings of leetcode simple problem

Joint set search: merge intervals and ask whether two numbers are in the same set

When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error

FFMpeg filter
随机推荐
有道云笔记
怎么用Kotlin去提高生产力:Kotlin Tips
Library management system based on SSM
JVM原理简介
PostgreSQL database high availability Patroni source code learning - etcd class
[Thesis Writing] how to write the overall design of JSP tourism network
2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination
C language series - Section 3 - functions
Crazy scientist
Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.
使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭
Arthas watch grabs a field / attribute of the input parameter
智能合约安全审计公司选型分析和审计报告资源下载---国内篇
Two drawing interfaces - 1 Matlab style interface
使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
Golang -- realize file transfer
Function introduction of member points mall system
Web security - CSRF (token)
Employee attendance management system based on SSM
使用BENCHMARKSQL工具对kingbaseES执行灌数据提示无法找到JDBC driver