当前位置:网站首页>Chapter 7 of JVM series -- bytecode execution engine
Chapter 7 of JVM series -- bytecode execution engine
2022-04-23 14:35:00 【InfoQ】
- All kinds of instructions expressed in binary code , be calledmachine instruction code. Start , People use it to write programs , This is machine language .
- Machine language can be understood and accepted by computers , But it's so different from people's language , It's not easy to be understood and remembered by people , And programming with it is easy to make mistakes .
- Once the program written with it is input into the computer ,CPU Read directly to run , So compared with programs written in other languages , Fastest execution .
- Machine instructions and CPU Closely related , So different kinds of CPU The corresponding machine instructions are different .
- Because the machine code has 0 and 1 A binary sequence of components , The readability is really poor , So people invented instructions .
- An instruction is a machine code specific to 0 and 1 Sequence , Reduced to the corresponding instruction ( It is usually abbreviated in English , Such as mov,inc etc. ), It's a little more readable
- Because of different hardware platforms , Perform the same operation , The corresponding machine code may be different , So the same instruction from different hardware platforms ( such as mov), The corresponding machine code may be different .
- Different hardware platforms , The instructions they support , There is a difference . So the instructions that each platform supports , Call it the instruction set of the corresponding platform .
- As is common :x86 Instruction set , The corresponding is x86 Architecture platform 、ARM Instruction set , The corresponding is ARM Architecture platform
- Because the readability of the instructions is still too poor , So people invented assembly language .
- In assembly language , Use mnemonics (Mnemonics) An operation code that replaces a machine instruction , Using address symbols (Symbol) Or labelling (Label) The address of an instruction or operand . On different hardware platforms , Assembly languages correspond to different machine language instruction sets , Conversion to machine instructions by assembly process .
- Because computers only know instruction codes , Therefore, the program written in assembly language must also be translated into machine instruction code , Computers can recognize and execute .
- In order to make it easier for computer users to program , Later, various high-level computer languages appeared .
- High level language is better than machine language 、 Assembly language is closer to human language when a computer executes a program written in a high-level language , Still need to interpret and compile the program into the machine's instruction code . The program that completes this process is called an interpreter or compiler .
- High level languages are not directly translated into Machine instructions , But translated into assembly language , As mentioned below C and C++

- The compilation process can be divided into two stages :Compile and assemble.
- The build process :Is to read the source program ( Character stream ), Analyze the morphology and grammar of it , Convert high-level language instructions into functional equivalent assembly code
- Assembly process :In fact, it refers to the process of translating assembly language code into target machine instructions .

- Bytecode is an intermediate state ( Middle ) The binary code of ( file ), It's more abstract than machine code , It needs to be translated by a literal translator before it can be machine code
- Bytecode is mainly used to realize specific software operation and software environment 、 Nothing to do with the hardware environment .
- Bytecode is implemented through compilers and virtual machines . The compiler compiles the source code into bytecode , Virtual machines on specific platforms translate bytecodes into instructions that can be executed directly .
- The typical application of bytecode is :Java bytecode

- Bytecode interpreterThe execution of bytecode is simulated by pure software code , Very inefficient .
- Template interpreterAssociate each bytecode with a template function , The template function directly generates the machine code when the bytecode is executed , This greatly improves the performance of the interpreter .
- Interpreter modular :The core function of the interpreter is realized
- Code modular :Used to manage HotSpot VM Local machine instructions generated at runtime
- The first isCompile the source code into a bytecode file, And then at run timeThe bytecode file is converted into machine code through the interpreter to execute
- The second isCompile implementation ( Directly compiled into machine code ). In order to improve the execution efficiency of modern virtual machine , Will use just in time compilation technology (JIT,Just In Time) Compile the method into machine code and then execute it
- Java Linguistic “ Compile time ” It's actually a paragraph “ Not sure ” Operation process , Because it may refer to aFront end compiler( Actually called “ The front end of the compiler ” More accurate ) hold .java The document is transformed into .class Documentation process ;
- It may also refer to the of virtual machinesBack end runtime compiler(JIT compiler ,Just In Time Compiler) The process of converting bytecode into machine code .
- It may also refer to the use ofStatic precompiler(AOT compiler ,Ahead of Time Compiler) Put... Directly .java The process of Compiling Files into local machine code .
- Java 9 The experimental method is introduced AOT Compiler tools aotc. It uses Graal compiler , Put in Java Class file to machine code , And stored in the generated dynamic shared library .>.java -> .class -> ( Use jaotc) -> .so
- benefits :Java The virtual machine load has been precompiled into a binary Library , Can be executed directly . You don't have to wait for the compiler to warm up in time , Reduce Java Application brings benefits to people “ Slow for the first time ” The bad experience of
- shortcoming :Destroyed java “ A compilation , Run anywhere ”, Must be for each different hardware ,OS Compile the corresponding distribution package .To reduce the Java The dynamics of linking process, The loaded code must be known in the compiler . We need to continue to optimize , At first, it only supported Linux X64 java base
- A method that is called many times , Or a method with more internal circulation can be called “ Hot code ”, Therefore, it can be passed JIT The compiler compiles into local machine instructions . Because this compilation occurs during the execution of a method , Therefore, it is called stack replacement , Or for shortOSR(On Stack Replacement) compile .
- How many times does a method have to be called , Or how many cycles does a loop body need to execute to reach this standard ? There must be a clear threshold ,JIT It's the compiler that will take this “ Hot code ” Compile for local machine instruction execution . It mainly depends onHot spot detection function .
- at present HotSpot VM The hot spot detection method is based on the counter .
- Using counter based hotspot detection ,HotSpot V It will be built for every method 2 There are two different types of counters , RespectivelyMethod call counter (Invocation Counter) It is used to count the number of calls of the method and the edge returning counter (Back Edge Counter) Used to count the number of cycles executed by the loop body .
- This counter is used to count the number of times a method is called , Its default threshold is Client In mode is 1500 Time , stay Server In mode is 10000 Time . Over this threshold , It will trigger JIT compile .
- This threshold can be determined by virtual machine parameters-XX:CompileThresholdTo set .
- When a method is called , We will first check whether the method has been JIT The compiled version , If there is , The compiled local code is preferred for execution . If there is no compiled version , Add... To the call counter value of this method 1, And then determineWhether the sum of the value of the method call counter and the back edge counter exceeds the threshold of the method call counter. If the threshold has been exceeded , A code compilation request for this method will be submitted to the just in time compiler .

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231433113805.html
边栏推荐
- 自动化的艺术
- 数组模拟队列进阶版本——环形队列(真正意义上的排队)
- 基于单片机的DS18B20的数字温度监控报警系统设计【LCD1602显示+Proteus仿真+C程序+论文+按键设置等】
- raised exception class EAccexxViolation with ‘Access violation at address 45EFD5 in module 出错
- 顺序栈的基本操作
- UML项目实例——抖音的UML图描述
- 利用 MATLAB 编程实现最速下降法求解无约束最优化问题
- Notes on Visio drawing topology
- C语言知识点精细详解——数据类型和变量【2】——整型变量与常量【1】
- After entering the new company, the operation and maintenance engineer can understand the deployment of the system from the following items
猜你喜欢
随机推荐
PWM speed regulation control system of DC motor based on 51 single chip microcomputer (with complete set of data such as Proteus simulation + C program)
MDS55-16-ASEMI整流模块MDS55-16
async void 导致程序崩溃
QT interface optimization: double click effect
51 Single Chip Microcomputer Design of traffic light system (with Proteus simulation, C program, schematic diagram, PCB, thesis and other complete data)
单片机的函数信号发生器,输出4种波形,频率可调,原理图,仿真和C程序
拼接hql时,新增字段没有出现在构造方法中
1 minute to understand the execution process and permanently master the for cycle (with for cycle cases)
Usage of BC
C语言p2选择分支语句详解
flannel 原理 之 子网划分
redis 模块编程中 key value的生命周期
Want to be an architect? Tamping the foundation is the most important
Use of ansible and common modules
面试官:说一下类加载的过程以及类加载的机制(双亲委派机制)
API gateway / API gateway (IV) - use of Kong - Integrated JWT and fuse plug-in
TLS/SSL 协议详解 (28) TLS 1.0、TLS 1.1、TLS 1.2之间的区别
8.5 循环神经网络简洁实现
Upgrade of openssh and modification of version number
【Servlet】Servlet 详解(使用+原理)









