如果在无限循环内声明任何 variable/object ,那么内存会怎样?

What will be the case in-respect to memory if any variable/object is declared inside an infinite loop?

我想知道以下场景在内存方面的表现。

此外

  1. 每次迭代后前一个对象会发生什么。

  2. 垃圾收集是否出现?如果是,那么在什么时候?

假设有一个人class。

public class Person {
    String name;    
    public Person() {
    this.name = "foo";
    }
}

而且我们在 main 方法中有一个无限循环

   while(true){
        Person p = new Person();
        System.out.println(p);
    }
  1. What will happen to the previous object after each iteration.

它将有资格进行 GC,因为它不再在范围内。

  1. Does Garbage Collection comes into picture? If yes, then at which point ?

因为这是一个短暂的对象,很快。甚至可能存在不需要 GC 做任何工作的优化(例如堆栈分配)。