0

When a for loop is added, the escape fails. JVM memory GC frequently。

On the surface, the CTX field should escape successfully.

However, when CTX is before and after the for loop, it has different results.

I'm not sure about the detailed implementation of the JVM, but there is no place to explain the reasons for the escape failure.

Both jvm8 and 14 have tried, and the result is the same.

Can someone tell me why?

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");
            // escape success
            ctx.doNothing();
            for (int c = 0; c < 4; c++) {

            }
            // escape fail
            // ctx.doNothing();
        }
    }
}

  • It seems like a bug to me, although escape analysis is a "best effort" feature anyhow. I tried your test with GraalVM, which has much better escape analysis, and it seemed to work correctly in both cases. – boneill May 05 '22 at 03:09
  • Yes, I can only think of it as a bug. – user6151223 May 05 '22 at 03:58
  • 1
    [OSR compilation](https://stackoverflow.com/questions/54405842/why-is-if-variable1-variable2-0-inefficient/54410675#54410675) is different from a regular JIT compilation. Furthermore, in your example OSR point is apparently different with and without an inner loop. It's not the right way to reason about JIT capabilities basing on OSR compilation. – apangin May 05 '22 at 05:30

0 Answers0