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();
}
}
}