为什么for循环会导致JVM逃脱失败
添加一个用于循环时,逃生失败。 JVM内存GC经常
在表面上,CTX字段应成功逃脱。
但是,当CTX在for循环之前和之后时,它的结果不同。
我不确定JVM的详细实现,但是没有地方可以解释逃生失败的原因。
JVM8和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");
// escape success
ctx.doNothing();
for (int c = 0; c < 4; c++) {
}
// escape fail
// ctx.doNothing();
}
}
}
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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论