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

原文地址:https://www.cnblogs.com/xianfengzhike/p/9128064.html

时间: 2024-07-31 19:35:00

JVM逃逸分析DoEscapeAnalysis的相关文章

JVM逃逸分析

JDK1.8默认开启逃逸分析,这是一种代码分析手段,能动态分析对象的作用域,为其它优化手段如栈上分配.标量替换和同步消除等提供依据. 一共可能有两种逃逸行为:方法逃逸和线程逃逸. 方法逃逸:当一个对象在方法中定义之后,作为参数传递到其它方法中: 线程逃逸:如类变量或实例变量,可能被其它线程访问到: 如果确认不存在逃逸行为,则可以对该对象进行如下优化:同步消除.标量替换和栈上分配. 参考:https://www.jianshu.com/p/20bd2e9b1f03 原文地址:https://www

逃逸分析

[分析对象动态作用域] 方法逃逸,线程逃逸. ——栈上分配:对象可以随着方法的结束而自动销毁. ——同步消除 ——标量替换:将对象中使用到的成员变量恢复原始类型来使用. ======================================================================= 在编程语言的编译优化原理中,分析指针动态范围的方法称之为逃逸分析.它跟静态代码分析技术中的指针分析和外形分析类似. 通俗一点讲,当一个对象的指针被多个方法或线程引用时,我们称这个指针发生

关于JVM的逃逸分析

何谓“逃逸”? 我们都知道Java中的对象默认是分配到堆上的,垃圾回收机制也会回收堆中不再使用的对象,但在此之前需要筛选可回收的对象,因此会造成,回收对象还有整理内存,都比较耗时间,开销也是非常之大.而此也是Java语言被疯狂吐槽的一地方,就是Java不支持栈上分配对象.而在我们日常开发中,内存,时间都是相当的宝贵,如何优化成为在开发中一个不可或缺的环节. 逃逸分析(Escape Analysis),是一种可以有效减少Java 程序中同步负载和内存堆分配压力的跨函数全局数据流分析算法.通过逃逸分

JVM的学习3_____逃逸分析与栈上分配

之前有提到过,为了提高GC的回收效率,对象实例的内存分配不一定必须存在于堆区中,还可采用堆外分配.而最常见的堆外分配就是采用逃逸分析筛选出未发生逃逸的对象,在栈帧中分配内存空间. 逃逸分析:就是分析出对象的作用域.当一个对象在方法体内声明后,该对象的引用被其他外部所引用时该对象就发生了逃逸,反之就会在栈帧中为对象分配内存空间. 根据逃逸分析在栈帧中分配的对象内存,不会使用GC进行垃圾回收.因为栈会随着方法的开始而创建,结束而销毁. 原文地址:https://www.cnblogs.com/xbf

深入理解Java中的逃逸分析

在Java的编译体系中,一个Java的源代码文件变成计算机可执行的机器指令的过程中,需要经过两段编译,第一段是把.java文件转换成.class文件.第二段编译是把.class转换成机器指令的过程. 第一段编译就是javac命令. 在第二编译阶段,JVM 通过解释字节码将其翻译成对应的机器指令,逐条读入,逐条解释翻译.很显然,经过解释执行,其执行速度必然会比可执行的二进制字节码程序慢很多.这就是传统的JVM的解释器(Interpreter)的功能.为了解决这种效率问题,引入了 JIT(即时编译)

十、逃逸分析和栈上分配

Java堆区已经不再是对象实例分配的唯一空间,可以在堆区之外分配内存以提升效率降低频率,逃逸分析即是如此. 什么是逃逸分析? 例如: 一个成员方法的内部实例化了一个对象,如果这个对象被方法外的引用指向了,那么就发生了逃逸现象.JVM在内存分配的时候会分析其是否发生逃逸,如果未发生逃逸的,那么就直接在栈上分配内存空间,其生命周期和线程相同.(也称之为"栈上分配") 原文地址:https://www.cnblogs.com/lay2017/p/8157760.html

逃逸分析(Escape Analysis)

什么是逃逸? 逃逸是指在某个方法之内创建的对象,除了在方法体之内被引用之外,还在方法体之外被其它变量引用到:这样带来的后果是在该方法执行完毕之后,该方法中创建的对象将无法被GC回收,由于其被其它变量引用.正常的方法调用中,方法体中创建的对象将在执行完毕之后,将回收其中创建的对象:故由于无法回收,即成为逃逸. /** * 无逃逸 */ void test01() { String test1 = "test1"; } String test2; /** * 逃逸 */ void test

[转]Java内存对象的逃逸分析

逃逸分析英文作Escape Analysis.在计算机语言编译器优化原理中,逃逸分析是指分析指针动态范围的方法,它同编译器优化原理的指针分析和外形分析相关联. 当变量(或者对象)在方法中分配后,其指针有可能被返回或者被全局引用,这样就会被其他过程或者线程所引用,这种现象称作指针(或者引用)的逃逸(Escape). 在Java 并发编程书里有个例子程序清单3-7 谈到 this escape. 开始没有想明白, 仔细琢磨了些时间发现代码主要的问题是在建构函数中创建了一个匿名类,然后发布了这个匿名类

java虚拟机的逃逸分析

逃逸分析作为其他优化手段提供依据的分析技术,其基本行为就是分析对象动态作用域:当一个对象在方法中被定义后,它可能被外部方法所引用,例如作为调用参数传递到其他方法中,称为方法逃逸.甚至还有可能被外部线程访问到,比如赋值给类变量或可以在其他线程中访问的实例变量,称为线程逃逸. 如果能证明一个对象不会逃逸到方法或线程之外,也就是别的方法或者线程无法通过任何途径访问到这个对象,则可能为这个变量进行一些高校的优化. 1)栈上分配,如果确定一个对象不会逃逸出方法之外,那么把这个对象在栈上分配内存,对象所占用