当前位置:网站首页>If the interviewer asks you about JVM, the extra answer of "escape analysis" technology will give you extra points
If the interviewer asks you about JVM, the extra answer of "escape analysis" technology will give you extra points
2022-07-27 05:46:00 【nuzzzzz】
I'm interviewing people ,JVM Memory model I almost have to ask , Although some people say that asking these questions is an interview to build an aircraft carrier , Working screw . If you want to be a CRUD Code the agriculture , You can choose not to know that .

stay JVM Q & A of memory model , Some people can say that objects are distributed on the heap . But when I ask the object must be stored on the heap , Most people say it's , Or hesitated .
In fact, it is correct to say that the object is allocated storage on the heap . But as the JIT The development of just in time compiler and the maturity of escape analysis technology , All objects are allocated to the heap, and it's becoming less absolute . On the stack , Scalar substitution , Some subtle changes will occur in optimization techniques such as lock elimination .
We know , We wrote Java Source code passed javac Compile into a bytecode file , Then the class loader loads the bytecode file into memory ,JVM Read and interpret the bytecode line by line, translate it into the corresponding machine instruction execution . Obviously , Explanation execution is better than those binary programs that can be executed directly ( for example C Language program ) Much slower .
So in order to improve efficiency , Introduced JIT ( Just in time compiler ) Optimization techniques .Java The program will still be interpreted and executed by the interpreter , But if a method or code block runs frequently ,JVM Think this is hot code , Then translate the hotspot code into local machine instructions , And optimize , cached , The next time you run this code, run it without further explanation .
JIT One of the most important optimization techniques is escape analysis (Escape Analysis).
Escape analysis
Escape analysis , In fact, it is to analyze whether an object will escape from the method , Analyze the dynamic scope of an object . If an object is defined within a method , And may be used by external references to methods , I think it escaped .
For example, the following person The object escapes , That is, it may be referenced externally by methods .
public Person personEscape() {
Person person = new Person();
return person;
}So why run away analysis , In fact, the ultimate goal is to optimize the program , Improve operational performance . There are the following optimization techniques :
- On the stack
- Scalar substitution
- Lock elimination
JDK1.7 Start , Escape analysis is on by default , It can be started and stopped by the following parameters .
# Turn on
-XX:+DoEscapeAnalysis
# close
-XX:-DoEscapeAnalysisOn the stack
If analyzing an object does not escape from the method , It could be assigned to the stack . So you don't have to do it in the heap GC Recycling , Improved performance
package com.chenpi;
/**
* @Description
* @Author Dried tangerine or orange peel
* @Date 2021/7/14
* @Version 1.0
*/
public class EscapeAnalysisTest {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++) {
stackAlloc();
}
System.out.println((System.currentTimeMillis() - startTime) + "ms");
}
public static void stackAlloc() {
Person person = new Person(" Dried tangerine or orange peel ", 18);
}
}
class Person {
private String name;
private long age;
public Person(String name, long age) {
this.name = name;
this.age = age;
}
}Virtual machine parameter settings enable escape analysis , And print GC journal .
-Xms200m -Xmx200m -XX:+DoEscapeAnalysis -XX:+PrintGCThe result of running the program is as follows , Consumption only requires 10 ms, And there's no GC .
10msClose escape analysis , And print GC journal .
-Xms200m -Xmx200m -XX:-DoEscapeAnalysis -XX:+PrintGCThe result of running the program is as follows , The consumption of time has increased 10 Many times , And with many times GC .
[GC (Allocation Failure) 51712K->784K(196608K), 0.0050396 secs]
[GC (Allocation Failure) 52496K->784K(196608K), 0.0030730 secs]
[GC (Allocation Failure) 52496K->752K(196608K), 0.0013993 secs]
[GC (Allocation Failure) 52464K->720K(196608K), 0.0018371 secs]
176msScalar substitution
- Scalar : Can't be broken down into smaller data types , For example, the basic data type is scalar .
- The amount of polymerization : It can be decomposed into other aggregate or scalar data types , For example, object reference types .
If an object doesn't escape , that JIT You can optimize the decomposition of this object into several scalars instead of . This is scalar substitution .
public void scalarReplace() {
Coordinates coordinates = new Coordinates(105.10, 80.22);
System.out.println(coordinates.longitude);
System.out.println(coordinates.latitude);
}The above demo program ,coordinates Objects don't escape , therefore JIT The compiler can use scalar substitution for optimization . Finally, it was optimized into the following program .
public void scalarReplace() {
System.out.println(105.10);
System.out.println(80.22);
}In fact, in the existing virtual machine , There's no real stack allocation , It's actually achieved by scalar substitution .
Lock elimination
Why eliminate locks ? Because locking reduces performance , So how not to lock is the best . If it is analyzed that the locked object will not escape , That is, it can only be accessed by one thread ,JIT It can be optimized to eliminate this lock . Also known as synchronous ellipsis .
public void lockRemove() {
synchronized (new Object()) {
System.out.println(" I'm tangerine peel !");
}
}The above demo program ,Object Objects don't escape , Therefore, it can only be accessed by the current thread , therefore JIT Compiler can optimize lock elimination . Finally, it was optimized into the following program .
public void lockRemove() {
System.out.println(" I'm tangerine peel !");
}summary
But as the JIT The development of just in time compiler and the maturity of escape analysis technology , All objects are allocated to the heap, and it's becoming less absolute . Through escape analysis technology , Objects may be assigned to the stack , Can reduce GC, Improve program performance .
But is the performance of the program with escape analysis turned on higher than that without ? Not necessarily . Escape analysis technology is also very complicated , So it's also a time-consuming process , If after the escape analysis , All the objects have escaped , We can't do optimization , So this escape analysis process takes time , It doesn't optimize , Do more harm than good .
边栏推荐
- 刷脸支付永远不会过时只会不断的变革
- dbswitch数据迁移数据增量时如何不覆盖目标源数据
- Exit login and JSX display
- The LAF protocol elephant of defi 2.0 may be one of the few profit-making means in your bear market
- How to judge whether a property belongs to an instance object or inherits from a constructor in JS
- The business logic of face brushing changed significantly, and merchants vied for war smoke to rise again
- What happens to the new arrow function in ES6
- When opening futures accounts, you should discuss the policy in detail with the customer manager
- Minimum handling charges and margins for futures companies
- 解析新时代所需要的创客教育DNA
猜你喜欢

Sealem Finance-基于Web3的全新去中心化金融平台

攻防世界-mfw

「PHP基础知识」使用echo语句输出信息

Exit login and JSX display

How can I get the lowest handling charge for opening a futures account?

NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon

「PHP基础知识」PHP语句和语句块

使用Docker部署Redis进行高可用主从复制

Several ways of element positioning in page layout

Permission configuration, component value transfer
随机推荐
[MRCTF2020]PYWebsite 1
Sequel Pro下载及使用方法
Minio分片上传解除分片大小限制 - chunk size must be greater than 5242880
去哪家期货公司如何开户?
AQUANEE将在近期登陆Gate以及BitMart,低位布局的良机
You can't even do a simple function test well. What do you take to talk about salary increase with me?
Differences between IsNaN and number.isnan in JS
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
Mysql分组后时并行统计数量
Aquanee will land in gate and bitmart in the near future, which is a good opportunity for low-level layout
「PHP基础知识」PHP语句和语句块
Js== mandatory type conversion provisions of operators
NFT新范式,OKALEIDO创新NFT聚合交易生态
「PHP基础知识」布尔型的使用
Read and understand the advantages of the LAAS scheme of elephant swap
选择正规的资质好的期货公司开户
软件测试常见面试题
什么是Alpha和Beta测试?
Return value of & (and) and | (or) operators in JS
Web3流量聚合平台Starfish OS,诠释真正商业的“P2E”生态