java - 如果在无限循环中声明了任何变量/对象,那么关于内存的情况会怎样?
问题描述
我想知道以下场景在内存方面的表现。
此外
每次迭代后前一个对象会发生什么。
垃圾收集会出现吗?如果是,那么在什么时候?
假设有一个人类。
public class Person {
String name;
public Person() {
this.name = "foo";
}
}
我们在 main 方法中有一个无限循环
while(true){
Person p = new Person();
System.out.println(p);
}
解决方案
- What will happen to the previous object after each iteration.
It will be eligible for GC, as it is no longer in scope.
- Does Garbage Collection comes into picture? If yes, then at which point ?
As this is a short-lived object, very soon. There might even be optimizations in place (such as stack allocation) that don't require the GC to do any work.
推荐阅读
- python - (Python)有没有办法分解脚本,以便输出可以用作同一脚本中另一个 UDF 的输入?
- javascript - 理解“this”绑定
- python - 自动将信息从 Dictionary 传输到 Anki
- android - 第一次在 ViewPager2 中滚动没有动画
- python - Pandas html 到 df - 数字中的逗号
- flutter - Flutter:列表视图中最喜欢的项目
- python - Python docx:如何从多个单词文件中提取两个固定字符串之间的表
- layout - 如何在 QML 中关闭带有特定按钮/区域的抽屉弹出窗口?
- php - 如何在宅基地机器上和本地服务时显示相同的 Laravel 路线
- c - 在字符串的某些位置获取字符