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


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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
JVM 逃逸分析(Escape Analysis)是一种用于分析 Java 代码中对象的作用域的技术。它可以帮助 JVM 在运行时确定对象的作用域,从而优化代码的性能。 在 Java 中,对象的生命周期是由它的作用域决定的。如果一个对象在方法内部创建并且只在该方法中使用,那么它的作用域就被限制在了该方法内部。这种对象被称为局部对象(Local Object)。如果一个对象在方法内部创建但是在方法外部被引用,那么它的作用域就被扩展到了方法外部。这种对象被称为逃逸对象(Escape Object)。 JVM 逃逸分析就是用来分析对象的作用域的。通过对代码的分析,JVM 可以确定哪些对象是局部对象,哪些对象是逃逸对象。对于局部对象,JVM 可以在栈上分配内存,这样可以避免在堆上分配内存的开销,从而提高代码的性能。对于逃逸对象,JVM 则必须在堆上分配内存。 JVM 逃逸分析对于代码性能的影响非常大。如果一个方法中大量使用逃逸对象,那么 JVM 将不得不频繁地在堆上分配内存,这将影响代码的性能。但是如果一个方法中大量使用局部对象,那么 JVM 可以将这些对象分配到栈上,从而避免在堆上分配内存的开销,这将大大提高代码的性能。 总之,JVM 逃逸分析是一种非常重要的技术,它可以帮助 JVM 在运行时优化代码的性能,提高代码的执行效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值