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
以上測試可以看出,棧上分配可以明顯提高效率