JVM逃逸分析DoEscapeAnalysis

Posted 先锋之客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JVM逃逸分析DoEscapeAnalysis相关的知识,希望对你有一定的参考价值。

JVM逃逸分析

JVM有栈、堆、方法区、本地栈等组成

 

栈:每个方法被执行的时候都会同时创建一个栈帧用于存储局部变量表、操作栈、动态连接、方法出口等信息。每个方法被调用直至执行完成的过程,就对应着一个栈帧在虚拟机栈中从入栈到出栈的过程。

堆:当实例化对象时,会把对象分配到堆中,然后把指向该堆的引用压入栈中。

逃逸:当一个对象的指针被多个方法或线程引用时,我们称这个指针发生了逃逸,一般情况返回对象、对全局变量的肤质一般都会发生逃逸。

逃逸分析:用来分析这种逃逸现象的方法称为逃逸分析

逃逸分析优化-栈上分配:栈上分配的意思是方法内局部变量(未发生逃逸)生成的实例在栈上分配,不用在堆中分配,分配完成后,继续在调用栈内执行,最后线程结束,栈空间被回收,局部变量对象也被回收。

代码测试:

import java.lang.management.ManagementFactory;
import java.util.List;
/** 
* 逃逸分析优化-栈上分配 
 * 栈上分配,意思是方法内局部变量(未发生逃逸)生成的实例在栈上分配,不用在堆中分配,分配完成后,继续在调用栈内执行,最后线程结束,栈空间被回收,局部变量对象也被回收。 
 * 一般生成的实例都是放在堆中的,然后把实例的指针或引用压入栈中。 
 *虚拟机参数设置如下,表示做了逃逸分析  消耗时间在10毫秒以下
 * -server  -Xmx10M  -Xms10M
    -XX:+DoEscapeAnalysis  -XX:+PrintGC
 *
 *虚拟机参数设置如下,表示没有做逃逸分析   消耗时间在1000毫秒以上
 * -server -Xmx10m  -Xms10m
     -XX: -DoEscapeAnalysis -XX:+PrintGC
 * @author 734621 
 * 
 */  

public class OnStack{
   public static void alloc(){
      byte[] b=new byte[2];
      b[0]=1;
   }
public static void main(String [] args){
    long b=System.currentTimeMillis();
    for(int i=0;i<100000000;i++){
       alloc();
      }
    long e=System.currentTimeMillis();
    System.out.println("消耗时间为:" + (e - b)); 
    List<String> paramters = ManagementFactory.getRuntimeMXBean().getInputArguments();  
      for(String p : paramters){  
          System.out.println(p);  
      }  
}
}

 

打印结果:

加逃逸分析的结果

[GC (Allocation Failure)  2816K->484K(9984K), 0.0013117 secs]
消耗时间为:7

-Xmx10m
-Xms10m
-XX:+DoEscapeAnalysis
-XX:+PrintGC
-Dfile.encoding=GBK

 

没有加逃逸分析的结果如下:

[GC (Allocation Failure)  3320K->504K(9984K), 0.0003174 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002524 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002618 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001474 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002843 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002922 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002190 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003259 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002738 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001946 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0028288 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0004558 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0106963 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002351 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001471 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002494 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002187 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002732 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001847 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002922 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002773 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002999 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002017 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001205 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002905 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002952 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002676 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001647 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001319 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001319 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002744 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002931 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001762 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001480 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002884 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001659 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002990 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003104 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0004854 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002767 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002489 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001392 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002272 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002641 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002826 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003180 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002714 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002166 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002749 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003793 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002362 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002714 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002764 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002981 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002723 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002324 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002647 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002591 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002875 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001820 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002729 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002931 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002251 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002676 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003130 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002143 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002881 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002603 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002556 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003966 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002749 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002949 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0006170 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0249173 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002620 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001914 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0028737 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0006000 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003945 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002313 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002881 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002544 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002140 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001773 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002650 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002943 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002201 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003274 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001381 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002442 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003031 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003465 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001577 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003189 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002609 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002348 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002216 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0009793 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001263 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002843 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002588 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002175 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0025132 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002579 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002491 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0005171 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003189 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002497 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002471 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001747 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0104052 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002840 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0009805 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0105928 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002620 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0038738 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002116 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002157 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0110542 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0104225 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002899 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002474 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001946 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003013 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002776 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003992 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003031 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002597 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003230 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003916 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002820 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002509 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002650 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002442 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0055639 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0109589 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0009693 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0020453 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0037897 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0109237 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002914 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002685 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0109944 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002720 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002644 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002638 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002471 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003101 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002518 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002858 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002752 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003453 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002609 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0108493 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002298 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0066162 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003078 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002615 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002673 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002532 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002659 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001762 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002937 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002234 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0009092 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002987 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002149 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002568 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002362 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002521 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001650 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003233 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002360 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001700 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002248 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0004145 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0008594 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0029256 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003189 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003497 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003242 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002116 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002837 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002931 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0002553 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0003579 secs]
[GC (Allocation Failure)  3320K->504K(9984K), 0.0001850 secs]

消耗时间为:1150
-Xmx10m
-Xms10m
-XX:-DoEscapeAnalysis
-XX:+PrintGC
-Dfile.encoding=GBK

以上测试可以看出,栈上分配可以明显提高效率

 

参照来源:https://blog.csdn.net/simba_1986/article/details/54599952

以上是关于JVM逃逸分析DoEscapeAnalysis的主要内容,如果未能解决你的问题,请参考以下文章

JVM逃逸分析

深入理解jvm原理之逃逸分析

Java - 你一定要知道的 JVM 逃逸分析

Java - 你一定要知道的 JVM 逃逸分析

一文秒懂JVM 逃逸分析,yyds

一文秒懂JVM 逃逸分析,yyds