性能问答>一个for循环带来的逃逸问题整懵了>
4回复
1年前

一个for循环带来的逃逸问题整懵了



一个对象与使用之间,插入一段循环,逃逸就会失败,算是记住一个知识点吧

java8 14 都尝试过,逃逸分析的逻辑应该一样

class Ctx {
    private String name;

     public Ctx(String name) {
         this.name = name;
     }

     public String getName() {
         return name;
     }

     public void doNothing() {

     }
 }

public class Test {
    public static void main(String[] args) {
        for (long i = 0; i < Long.MAX_VALUE; ++i) {
            Ctx ctx = new Ctx("name");
            // 逃逸成功
            ctx.doNothing();
            for (int c = 0; c < 4; c++) {

            }
            // 会逃逸失败
            // ctx.doNothing();
        }
    }
}

逃逸失败的记录

java -Xmx30M -XX:+PrintEscapeAnalysis -XX:+PrintEliminateAllocations -XX:+PrintInlining  -XX:+Verbose Test

                              @ 1   java.lang.CharacterData::of (120 bytes)                              @ 17   java.lang.AbstractStringBuilder::newCapacity (39 bytes)   callee is too large
                              @ 4                                @ 20   java.util.Arrays::copyOf (19 bytes)
                                @ 11   java.lang.Math::min (11 bytes)
   callee is too large
                                      @ 5   java.lang.CharacterData::toLowerCase (0 bytes)   no static binding
                              @ 66   java.lang.String::indexOfSupplementary java.lang.CharacterDataLatin1::getProperties (71 bytes)   callee is too large
                        @ 14   java.lang.System::arraycopy (0 bytes)   intrinsic
 (11 bytes)
                              @ 7   java.lang.AbstractStringBuilder::ensureCapacityInternal (27 bytes)
                              @ 7   java.lang.AbstractStringBuilder::append (29 bytes)
                                             @ 7   java.lang.AbstractStringBuilder::ensureCapacityInternal (27 bytes)
                              @ 9  java/lang/StringIndexOutOfBoundsException                                  @ 17   java.lang.AbstractStringBuilder::newCapacity                   @ 17   java.lang.AbstractStringBuilder::newCapacity (39 bytes)   callee is too large
 (39 bytes)   callee is too large
::<init> (not loaded)   not inlineable
                                @ 20   java.util.Arrays::copyOf (19 bytes)
                                  @ 20   java.util.Arrays::copyOf                              @ 27  java/lang/StringIndexOutOfBoundsException::<init> (not loaded)   not inlineable
 (19 bytes)
                                  @ 11   java.lang.Math::min                              @ 43  java/lang/StringIndexOutOfBoundsException::<init> (not loaded)   not inlineable
 (11 bytes)
                                    @ 11   java.lang.Math::min                              @ 58   java.lang.System::arraycopy (11 bytes)
                                                                      @ 14   java.lang.System::arraycopy (0 bytes)   intrinsic
 (0 bytes)   intrinsic
@ 14   java.lang.System::arraycopy (0 bytes)   intrinsic
                              @ 1   java.lang.Object::<init> (1 bytes)
                              @ 1   java.lang.Object::<init> (1 bytes)   inline (hot)
                              @ 16   Ctx::<init> (10 bytes)
                                @ 1   java.lang.Object::<init> (1 bytes)
                              @ 36   Ctx::doNothing (1 bytes)
                              @ 16   Ctx::<init> (10 bytes)
                                @ 1   java.lang.Object::<init> (1 bytes)
                              @ 36   Ctx::doNothing (1 bytes)
EA: 2 iterations to build connection graph with 276 nodes and worklist size 21

======== Connection graph for  Test::main
JavaObject NoEscape(NoEscape) NSR [ 235F [ 184 189 272 ]]   172 Allocate        ===  161  268  132  8  1 ( 28  45  20  1  1  152  1  1  1 ) [[ 173  174  175  182  183  184 ]]  rawptr:NotNull ( int:>=0, java/lang/Object:NotNull *, bool, top ) Test::main @ bci:10  Type:{0:control, 1:abIO, 2:memory, 3:rawptr:BotPTR, 4:return_address, 5:rawptr:NotNull} !jvms: Test::main @ bci:10
LocalVar NoEscape(NoEscape) [ 172P [ 189 235b ]]   184  Proj    ===  172  [[ 185  189  235 ]] #5  Type:rawptr:NotNull !jvms: Test::main @ bci:10
LocalVar NoEscape(NoEscape) [ 184 172P [ 272 ]]   189   CheckCastPP     ===  186  184  [[ 238  272 ]]   Oop:Ctx:NotNull:exact * !orig=[265],[111],[126] !jvms: Test::main @ bci:10
LocalVar NoEscape(NoEscape) [ 47 189 1P 48P 172P [ ]]   272     Phi     ===  267  47  189  [[ 133  124 ]]   Oop:Ctx:BotPTR * !orig=[111],[126] !jvms: Test::main @ bci:23

=== No allocations eliminated for  Test::main since there are no scalar replaceable candidates ===
                              @ 36   Ctx::doNothing (1 bytes)   inline (hot)
                              @ 16   Ctx::<init> (10 bytes)   inline (hot)
                                @ 1   java.lang.Object::<init> (1 bytes)   inline (hot)

成功逃逸的记录

java -Xmx30M -XX:+PrintEscapeAnalysis -XX:+PrintEliminateAllocations -XX:+PrintInlining  -XX:+Verbose Test

======== Connection graph for  Test::main
JavaObject NoEscape(NoEscape) [ 160F [ 108 113 ]]   96  Allocate        ===  84  72  76  8  1 ( 28  94  20  1  1  75  1  1  1 ) [[ 97  98  99  106  107  108 ]]  rawptr:NotNull ( int:>=0, java/lang/Object:NotNull *, bool, top ) Test::main @ bci:10  Type:{0:control, 1:abIO, 2:memory, 3:rawptr:BotPTR, 4:return_address, 5:rawptr:NotNull} !jvms: Test::main @ bci:10
LocalVar NoEscape(NoEscape) [ 96P [ 113 160b ]]   108   Proj    ===  96  [[ 109  113  160 ]] #5  Type:rawptr:NotNull !jvms: Test::main @ bci:10
LocalVar NoEscape(NoEscape) [ 108 96P [ ]]   113        CheckCastPP     ===  110  108  [[ 163 ]]   Oop:Ctx:NotNull:exact * !jvms: Test::main @ bci:10

Scalar  113     CheckCastPP     ===  110  108  [[ 163 ]]   Oop:Ctx:NotNull:exact *,iid=96 !jvms: Test::main @ bci:10
++++ Eliminated: 96 Allocate
                              @ 16   Ctx::<init> (10 bytes)   inline (hot)
                                @ 1   java.lang.Object::<init> (1 bytes)   inline (hot)
                              @ 21   Ctx::doNothing (1 bytes)   inline (hot)
EA: 2 iterations to build connection graph with 227 nodes and worklist size 13

======== Connection graph for  Test::main
JavaObject NoEscape(NoEscape) [ 181F [ 129 134 ]]   117 Allocate        ===  105  219  90  8  1 ( 28  115  20  1  1  96  1  1  1 ) [[ 118  119  120  127  128  129 ]]  rawptr:NotNull ( int:>=0, java/lang/Object:NotNull *, bool, top ) Test::main @ bci:10  Type:{0:control, 1:abIO, 2:memory, 3:rawptr:BotPTR, 4:return_address, 5:rawptr:NotNull} !jvms: Test::main @ bci:10
LocalVar NoEscape(NoEscape) [ 117P [ 134 181b ]]   129  Proj    ===  117  [[ 130  134  181 ]] #5  Type:rawptr:NotNull !jvms: Test::main @ bci:10
LocalVar NoEscape(NoEscape) [ 129 117P [ ]]   134       CheckCastPP     ===  131  129  [[ 184 ]]   Oop:Ctx:NotNull:exact * !jvms: Test::main @ bci:10

Scalar  134     CheckCastPP     ===  131  129  [[ 184 ]]   Oop:Ctx:NotNull:exact *,iid=117 !jvms: Test::main @ bci:10
++++ Eliminated: 117 Allocate
                              @ 16   Ctx::<init> (10 bytes)   inline (hot)
                                @ 1   java.lang.Object::<init> (1 bytes)   inline (hot)
                              @ 21   Ctx::doNothing (1 bytes)   inline (hot)
EA: 2 iterations to build connection graph with 254 nodes and worklist size 12

======== Connection graph for  Test::main
JavaObject NoEscape(NoEscape) [ 137F [ 85 90 ]]   73    Allocate        ===  244  65  241  8  1 ( 71  70  25  1  1  67  1  1  1 ) [[ 74  75  76  83  84  85 ]]  rawptr:NotNull ( int:>=0, java/lang/Object:NotNull *, bool, top ) Test::main @ bci:10  Type:{0:control, 1:abIO, 2:memory, 3:rawptr:BotPTR, 4:return_address, 5:rawptr:NotNull} !jvms: Test::main @ bci:10
LocalVar NoEscape(NoEscape) [ 73P [ 90 137b ]]   85     Proj    ===  73  [[ 86  90  137 ]] #5  Type:rawptr:NotNull !jvms: Test::main @ bci:10
LocalVar NoEscape(NoEscape) [ 85 73P [ ]]   90  CheckCastPP     ===  87  85  [[ 140 ]]   Oop:Ctx:NotNull:exact * !jvms: Test::main @ bci:10

Scalar  90      CheckCastPP     ===  87  85  [[ 140 ]]   Oop:Ctx:NotNull:exact *,iid=73 !jvms: Test::main @ bci:10
++++ Eliminated: 73 Allocate
                              @ 16   Ctx::<init> (10 bytes)   inline (hot)
                                @ 1   java.lang.Object::<init> (1 bytes)   inline (hot)
                              @ 21   Ctx::doNothing (1 bytes)   inline (hot)

 
592 阅读
请先登录,查看4条精彩评论吧
快去登录吧,你将获得
  • 浏览更多精彩评论
  • 和开发者讨论交流,共同进步