jvm 優(yōu)化篇-(4)-棧上分配與逃逸分析 -XX:+DoEscapeAnalysis -XX:+UseTLAB -XX:TLABRefillWasteFraction

逃逸分析(Escape Analysis)

???????逃逸分析的基本行為就是分析對象動態(tài)作用域:當(dāng)一個對象在方法中被定義后,它可能被外部方法所引用,稱為方法逃逸。

方法逃逸的幾種方式如下:
public class EscapeTest {
    public static Object obj;

    // 給全局變量賦值,發(fā)生逃逸
    public void globalVariableEscape() {  
        obj = new Object();
    }

    // 方法返回值,發(fā)生逃逸
    public Object methodEscape() {  
        return new Object();
    }

    // 實(shí)例引用發(fā)生逃逸
    public void instanceEscape() {  
        test(this); 
    }
}

棧上分配

???????棧上分配是Java虛擬機(jī)提供的一種優(yōu)化技術(shù),基本思想是:"對于那些線程私有的對象(指的是不可能被其他線程訪問的對象),可以將它們直接分配在棧上,而不是分配在堆上"。分配在棧上的好處:可以在函數(shù)調(diào)用結(jié)束后自行銷毀,而不需要垃圾回收器的介入,減輕GC壓力,從而提升系統(tǒng)的性能。

使用場景
  • 線程私有對象。
  • 受虛擬機(jī)??臻g的約束,適用小對象,大對象無法觸發(fā)虛擬機(jī)棧上分配(后面有demo來佐證)。
    線程私有變量,大對象虛擬機(jī)會分配到TLAB中,TLAB(Thread Local Allocation Buffer)要不要了解下?
    <<<<<<傳送門
  • 在棧上分配該對象的內(nèi)存,當(dāng)棧幀從Java虛擬機(jī)棧中彈出,就自動銷毀這個對象。減小垃圾回收器壓力。
1、虛擬機(jī)內(nèi)存邏輯圖
image.png
2、JVM內(nèi)存分配源碼:
CASE(_new): {
        u2 index = Bytes::get_Java_u2(pc+1);
        ConstantPool* constants = istate->method()->constants();
        // 如果目標(biāo)Java類已經(jīng)解析
        if (!constants->tag_at(index).is_unresolved_klass()) {
          // Make sure klass is initialized and doesn't have a finalizer
          Klass* entry = constants->slot_at(index).get_klass();
          assert(entry->is_klass(), "Should be resolved klass");
          Klass* k_entry = (Klass*) entry;
          assert(k_entry->oop_is_instance(), "Should be InstanceKlass");
          InstanceKlass* ik = (InstanceKlass*) k_entry;
          // 如果符合快速分配場景
          if ( ik->is_initialized() && ik->can_be_fastpath_allocated() ) {
            size_t obj_size = ik->size_helper();
            oop result = NULL;
            // If the TLAB isn't pre-zeroed then we'll have to do it
            bool need_zero = !ZeroTLAB;
            if (UseTLAB) {
              result = (oop) THREAD->tlab().allocate(obj_size);
            }
            // 如果TLAB分配失敗,就在Eden區(qū)分配
            if (result == NULL) {
              need_zero = true;
              // Try allocate in shared eden
        retry:
              // 指針碰撞分配
              HeapWord* compare_to = *Universe::heap()->top_addr();
              HeapWord* new_top = compare_to + obj_size;
              if (new_top <= *Universe::heap()->end_addr()) {
                if (Atomic::cmpxchg_ptr(new_top, Universe::heap()->top_addr(), compare_to) != compare_to) {
                  goto retry;
                }
                result = (oop) compare_to;
              }
            }
            if (result != NULL) {
              // Initialize object (if nonzero size and need) and then the header
              // TLAB區(qū)清零
              if (need_zero ) {
                HeapWord* to_zero = (HeapWord*) result + sizeof(oopDesc) / oopSize;
                obj_size -= sizeof(oopDesc) / oopSize;
                if (obj_size > 0 ) {
                  memset(to_zero, 0, obj_size * HeapWordSize);
                }
              }
              if (UseBiasedLocking) {
                result->set_mark(ik->prototype_header());
              } else {
                result->set_mark(markOopDesc::prototype());
              }
              result->set_klass_gap(0);
              result->set_klass(k_entry);
              // 將對象地址壓入操作數(shù)棧棧頂
              SET_STACK_OBJECT(result, 0);
              // 更新程序計(jì)數(shù)器PC,取下一條字節(jié)碼指令,繼續(xù)處理
              UPDATE_PC_AND_TOS_AND_CONTINUE(3, 1);
            }
          }
        }
        // Slow case allocation
        // 慢分配
        CALL_VM(InterpreterRuntime::_new(THREAD, METHOD->constants(), index),
                handle_exception);
        SET_STACK_OBJECT(THREAD->vm_result(), 0);
        THREAD->set_vm_result(NULL);
        UPDATE_PC_AND_TOS_AND_CONTINUE(3, 1);
      }

代碼總體邏輯:JVM再分配內(nèi)存時,總是優(yōu)先使用快分配策略,當(dāng)快分配失敗時,才會啟用慢分配策略。

  • 1、如果Java類沒有被解析過,直接進(jìn)入慢分配邏輯。
  • 2、快速分配策略,如果沒有開啟棧上分配或者不符合條件則會進(jìn)行TLAB分配。
  • 3、快速分配策略,如果TLAB分配失敗,則嘗試Eden區(qū)分配。
  • 4、如果Eden區(qū)分配失敗,則進(jìn)入滿分配策略。
  • 5、如果對象滿足直接進(jìn)入老年代的條件,那就直接進(jìn)入老年代分配。
  • 6、快速分配,對于熱點(diǎn)代碼,如果開啟逃逸分析,JVM自會執(zhí)行棧上分配或者標(biāo)量替換等優(yōu)化方案。
3、佐證JVM在某些場景使用棧上分配

設(shè)置JVM運(yùn)行參數(shù):-Xmx10m -Xms10m -XX:+DoEscapeAnalysis -XX:-UseTLAB -XX:+PrintGC

/**
 * @description 開啟逃逸模式,關(guān)閉線程本地緩存模式(TLAB)(jdk1.8默認(rèn)開啟)
 * @author biudefu
 * @since 2019/8/13  上午6:55
 * -Xmx10m -Xms10m    -XX:+DoEscapeAnalysis  -XX:-UseTLAB  -XX:+PrintGC  
 */
public class AllocationOnStack {

    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        for (int index = 0; index < 100000000; index++) {
            allocate();
        }
        long end = System.currentTimeMillis();
        System.out.println((end - start)+" ms");
        Thread.sleep(1000*1000);
        // 看后臺堆情況,來佐證關(guān)閉逃逸優(yōu)化后,是走的堆分配。
    }

    public static void allocate() {
        byte[] bytes = new byte[2];
        bytes[0] = 1;
        bytes[1] = 1;
    }
}
運(yùn)行結(jié)果:

[GC (Allocation Failure)  2048K->520K(9728K), 0.0008938 secs]
[GC (Allocation Failure)  2568K->520K(9728K), 0.0006386 secs]
6 ms

jstat -gc pid ,查看內(nèi)存使用情況:


開啟逃逸模式,關(guān)閉TLAB

調(diào)整JVM運(yùn)行參數(shù)-Xmx10m -Xms10m -XX:-DoEscapeAnalysis -XX:+UseTLAB -XX:+PrintGC

運(yùn)行結(jié)果:
[GC (Allocation Failure)  2048K->504K(9728K), 0.0013831 secs]
[GC (Allocation Failure)  2552K->512K(9728K), 0.0010576 secs]
[GC (Allocation Failure)  2560K->400K(9728K), 0.0022408 secs]
[GC (Allocation Failure)  2448K->448K(9728K), 0.0006095 secs]
[GC (Allocation Failure)  2496K->416K(9728K), 0.0010540 secs]
[GC (Allocation Failure)  2464K->464K(8704K), 0.0007620 secs]
[GC (Allocation Failure)  1488K->381K(9216K), 0.0007714 secs]
[GC (Allocation Failure)  1405K->381K(9216K), 0.0004409 secs]
[GC (Allocation Failure)  1405K->381K(9216K), 0.0004725 secs]
.......
[GC (Allocation Failure)  2429K->381K(9728K), 0.0008293 secs]
[GC (Allocation Failure)  2429K->381K(9728K), 0.0009006 secs]
[GC (Allocation Failure)  2429K->381K(9728K), 0.0005553 secs]
[GC (Allocation Failure)  2429K->381K(9728K), 0.0005077 secs]
894 ms

jstat -gc pid ,查看內(nèi)存使用情況:


關(guān)閉逃逸模式,開啟TLAB模式

調(diào)整JVM運(yùn)行參數(shù)-Xmx10m -Xms10m -XX:-DoEscapeAnalysis -XX:-UseTLAB -XX:+PrintGC

運(yùn)行結(jié)果:
[GC (Allocation Failure)  2048K->472K(9728K), 0.0007073 secs]
[GC (Allocation Failure)  2520K->528K(9728K), 0.0009216 secs]
[GC (Allocation Failure)  2576K->504K(9728K), 0.0005897 secs]
[GC (Allocation Failure)  2551K->424K(9728K), 0.0005780 secs]
[GC (Allocation Failure)  2472K->440K(9728K), 0.0006923 secs]
[GC (Allocation Failure)  2488K->456K(8704K), 0.0006277 secs]
[GC (Allocation Failure)  1480K->389K(9216K), 0.0005560 secs]
.......
[GC (Allocation Failure)  2437K->389K(9728K), 0.0003227 secs]
[GC (Allocation Failure)  2437K->389K(9728K), 0.0004264 secs]
[GC (Allocation Failure)  2437K->389K(9728K), 0.0004396 secs]
[GC (Allocation Failure)  2437K->389K(9728K), 0.0002773 secs]
[GC (Allocation Failure)  2437K->389K(9728K), 0.0002766 secs]
1718 ms
關(guān)閉逃逸,關(guān)閉TLAB

運(yùn)行結(jié)果對比:
1、運(yùn)行耗時(開啟逃逸 VS關(guān)閉逃逸(開啟TLAB)VS關(guān)閉逃逸(關(guān)閉TLAB)):
???? 6ms VS 894ms VS 1718ms
2、虛擬機(jī)內(nèi)存&回收??(開啟逃逸VS關(guān)閉逃逸):

堆內(nèi)存&YoungGC回收??對比


總結(jié):

啟動參數(shù) JVM內(nèi)存分配模式 Eden區(qū) YoungGC 耗時
-XX:+DoEscapeAnalysis(開逃逸分析)-XX:+UseTLAB (開啟TLAB) 虛擬機(jī)棧上分配模式(小對象) 較少使用 較少使用 很低
-XX:-DoEscapeAnalysis(關(guān)閉逃逸分析)-XX:+UseTLAB(開啟TLAB) TLAB區(qū)分配模式 大量使用 大量使用 較高
-XX:-DoEscapeAnalysis(關(guān)閉逃逸分析)-XX:-UseTLAB(關(guān)閉TLAB) Eden區(qū)分配模式 大量使用 大量使用 特別高
整個對比會很疑惑?

?????-XX:-DoEscapeAnalysis -XX:-UseTLAB VS -XX:-DoEscapeAnalysis -XX:+UseTLAB 耗時為何相差這么多?
?????原因就在TLAB分配與Eden區(qū)分配存在差異,TLAB(Thread Local Allocation Buffer)是在共享堆上安全分配,沒有指針碰撞!<<<<<<傳送門


調(diào)整分配空間大?。?/h6>
/**
 * @author biudefu
 * @since 2019/8/13  上午6:55
 * -Xmx10m -Xms10m    -XX:-DoEscapeAnalysis -XX:+UseTLAB  -XX:+PrintCommandLineFlags -XX:+PrintGC 
 */
public class AllocationOnStack {

    private static final int _1B =  65;

    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        for (int index = 0; index < 100000000; index++) {
            allocateBigSpace();
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        Thread.sleep(1000*1000);
        // 看后臺堆情況,來佐證關(guān)閉逃逸優(yōu)化后,是走的堆分配。
    }

    public static void allocate() {
        byte[] bytes = new byte[2];
        bytes[0] = 1;
        bytes[1] = 1;
    }
    public static void allocateBigSpace() {
        byte[] allocation1;
        allocation1 = new byte[1 * _1B];
      
    }

}
運(yùn)行結(jié)果:
-XX:+DoEscapeAnalysis -XX:InitialHeapSize=5242880 -XX:MaxHeapSize=5242880 -XX:+PrintCommandLineFlags -XX:+PrintGC -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC -XX:-UseTLAB 
[GC (Allocation Failure)  1023K->516K(5632K), 0.0028410 secs]
[GC (Allocation Failure)  1540K->578K(5632K), 0.0023265 secs]
........
[GC (Allocation Failure)  2466K->1442K(5632K), 0.0013395 secs]
[GC (Allocation Failure)  2466K->1442K(5632K), 0.0004367 secs]
8925

調(diào)整啟動參數(shù): -XX:+DoEscapeAnalysis -XX:-UseTLAB

運(yùn)行結(jié)果:
-XX:+DoEscapeAnalysis -XX:InitialHeapSize=5242880 -XX:MaxHeapSize=5242880 -XX:+PrintCommandLineFlags -XX:+PrintGC -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC -XX:-UseTLAB 
[GC (Allocation Failure)  1023K->516K(5632K), 0.0028410 secs]
[GC (Allocation Failure)  1540K->578K(5632K), 0.0023265 secs]
........
[GC (Allocation Failure)  2466K->1442K(5632K), 0.0013395 secs]
[GC (Allocation Failure)  2466K->1442K(5632K), 0.0004367 secs]
8925
經(jīng)過對比得出結(jié)論:

分配內(nèi)存為>64byte == -XX:-UseTLAB


經(jīng)過多次測試發(fā)現(xiàn)當(dāng)_1B=64b時效率還是非常高,一旦大于64b就會急劇下降。所以推斷出64byte是JVM選擇是TLAB分配 OR Eden區(qū)分配的臨界值。
(測試本機(jī)配置:MacBook Pro (Retina, 15-inch, Mid 2015)系統(tǒng)版本MacOS Mojave)

TLAB(Thread Local Allocation Buffer)為什么超過64byte對象不分配在TLAB區(qū)域,要不要了解下?<<<<<<傳送門

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容