当前位置:网站首页>JVM (4) bytecode technology + runtime optimization
JVM (4) bytecode technology + runtime optimization
2022-06-29 19:21:00 【Demon_ bh】
Bytecode Technology
1、 Class file structure
ca fe ba be 00 00 00 34 00 23 0a 00 06 00 15 09
- front 4 Bytes are magic numbers :cafababe
- Come down 2 Bytes are minor version numbers :00 00
- Come down 2 Bytes are the large version number :00 34 —> 52 ( Corresponding jdk1.8, 45 by jdk1.1)
2、 Bytecode instruction
Use javap Tools decompile class file :
javap -v D:Demo.class
3、 Method execution process

- Virtual machine stack : Method local variable table + The stack of operands + Dynamic connection + The return address
- The table of local variables is represented by slot Is the smallest unit , Store method parameters and local variables
4、 exception handling
finally The bytecode in is copied 3 Share , Put in separately try technological process ,catch Process and catch The remaining exception type flow .
Be careful : Although from the perspective of bytecode instructions , There are... In each block finally block , however finally The code in the block will only be executed once
public class Code_18_FinallyReturnTest {
public static void main(String[] args) {
int i = Code_18_FinallyReturnTest.test();
// The result is 20
System.out.println(i);
}
public static int test() {
int i;
try {
i = 10;
return i;
} finally {
i = 20;
return i;
}
}
}
- If in finally In the return, It'll swallow the anomaly
- finally There is return, Will be covered try Medium return
- Bytecode displays exception information in Anomaly table in
5、 Grammatical sugar
Default constructor
Automatic disassembly box (jdk1.5 You can't automatically )
Generic erase
Variable parameters (public static void foo(String… args){})
foreach() Automatic conversion to for(i=0; i<n;i++) Or an iterator of a collection Iterator Traverse
switch
try-with-resources( Omit the finally Close resources , And Can keep exceptions when closing resources )
The bridge method when the method is overridden ( A subclass return value can be a subclass of a parent return value –>jvm Automatic conversion )
Anonymous inner class ( Automatically generate a class )
Operation period optimization
1) Just in time compilation
HotSpot Interpreter and compiler coexist :
For most of the code, the method of interpretation and execution is adopted ;
For a small number of hot code that runs frequently , Compile it into machine code to improve execution efficiency , To achieve the desired operating speed .
Layered compilation
JVM The execution state is divided into 5 A hierarchical :
0 layer : Explain to perform , Translate bytecode into machine code with interpreter
1 layer : Use C1 Just in time compiler compilation execution ( No profiling)
2 layer : Use C1 Just in time compiler compilation execution ( With basic profiling)
3 layer : Use C1 Just in time compiler compilation execution ( With complete profiling)
4 layer : Use C2 Just in time compiler compilation execution
- Interpreter :
- Interpret bytecode as machine code , Next time you encounter the same bytecode , Repeated explanations will still be performed
- Interpret bytecode as machine code common to all platforms
- Just in time compiler
- Compile some bytecode into machine code , And deposit in Code Cache, Next time you come across the same code , Direct execution , No need to compile
- Depending on the platform type , Generate platform specific machine code
A simple comparison of execution efficiency Interpreter < C1(Client) < C2(Server).
2) Escape analysis (Escape Analysis)
Java Hotspot The virtual machine analyzes the dynamic scope of the newly created object , And decide whether it is Java A technique for allocating memory on the heap .
When analyzing the An object cannot escape outside a method or thread , Perform the following optimizations :
- On the stack ( Memory is allocated directly on the stack , The object is destroyed along with the stack frame )
- Synchronous elimination
- Scalar substitution ( Don't create objects , Create only the member variables that you use )
3) Method Inlining
If JVM It is detected that some small methods are executed frequently , It will replace the method call with the method body itself , Such as :
private int add4(int x1, int x2, int x3, int x4) {
// Here we call add2 Method
return add2(x1, x2) + add2(x3, x4);
}
private int add2(int x1, int x2) {
return x1 + x2;
}
After the method call is replaced
private int add4(int x1, int x2, int x3, int x4) {
// Replaced by the method itself
return x1 + x2 + x3 + x4;
}
边栏推荐
猜你喜欢
随机推荐
测试方法学习
洞见科技作为「唯一」隐私计算数商,「首批」入驻长三角数据要素流通服务平台
软件工程专业大二,之前的学习情况不太好该怎么规划后续发展路线
Point, line, surface and body of enterprise digital transformation!
Element waiting mechanism
Flutter 调用百度地图APP实现位置搜索、路线规划
有了这4个安全测试工具,对软件安全测试say so easy!
Page object and data driven test
go: 如何编写一个正确的udp服务端
自动获取本地连接及网络地址修改
docker compose 部署Flask项目并构建redis服务
Win11系统小组件打不开?Win11系统小组件无法打开解决方法
Solution of portable emergency communication system for flood control and rescue
3-2主机发现-三层发现
求职大厂被拒?腾讯高手总结了11条被拒的原因!
云上未来,数智导航:阿里云研究院报告合集
The concept and properties of mba-day26 number
74. maximum profit on shares
[software testing] 01 -- software life cycle and software development model
Intégration d'outils et de cadres tiers









