在重复调用的方法中创建的对象会发生什么?

huangapple go评论72阅读模式
英文:

What happens to objects created in a method which is called repeatedly?

问题

如果我通过调用一个方法(接受一个参数)来创建一个对象,但我多次调用该方法,每次传递一个新的参数。

在上面的代码中,如果我多次调用该方法,每次传递一个唯一的狗名,那么最后的狗变量只会指向方法最后接收到的名字的狗对象。

我的问题是前面的狗对象会发生什么?它们仍然在堆内存中,但没有办法检索它们吗?

感谢您抽出时间阅读这个问题。非常感谢您的帮助。

英文:

Here goes the question:

If I am creating a single object by calling a method (accepting say a single parameter) but I am calling the method multiple times passing a new argument each time.

void create(String name) {

		// Create a dog
		Dog dog = new Dog(name);
}

On the above code, if I call the method multiple times passing a unique dog name each time, the dog variable, at last, is only going to point to the Dog object with the name that the method received at last.

My question is what happens to the previous dog objects? Are they still in heap memory but there's nothing to retrieve it?

Thank you for taking the time to read this. Your help is much appreciated

答案1

得分: 3

每当您执行 new 关键字时,JVM 会在堆上为存储该实例的对象分配一个新的内存块。

如果我通过调用一个方法(接受一个参数)创建一个对象,但我多次调用该方法每次传递一个新的参数,那么之前的对象会发生什么?它们是否仍然在堆内存中,只是没有东西可以检索它们?

每次调用方法时,当前线程的堆栈内存将分配一个新的栈帧,并且您的对象引用将在该栈帧的范围内声明,这意味着只要您的方法帧存在,该对象引用就不会符合垃圾回收(GC)的条件。但在方法返回/退出后,它将变为可以进行GC的对象。

唯一的例外情况是,在方法中创建的对象不会被GC清除的情况是,如果您将其引用传递到其他地方。在这种情况下,对象仍然有引用指向它,因此堆将继续维护它,不会对其进行GC处理。

注意,您无法控制GC何时运行,对象可能会在方法返回后立即销毁,或者经过一段(通常很短的)时间后销毁;无论哪种情况,方法返回后您都失去了对对象的引用,这意味着它最终会被GC处理。

英文:

Anytime you execute new keyword, JVM allocates a new memory chunk, on the Heap, for storing the instance.

> If I am creating a single object by calling a method (accepting say a single parameter) but I am calling the method multiple times passing a new argument each time. My question is what happens to the previous dog objects? Are they still in heap memory but there's nothing to retrieve it?

Each time you will call your method, a new stack frame will be allocated on the Stack Memory of the current thread, and your object reference will be declared within the scope of that frame, which means, that it will be ineligible for Garbage Collection (GC) as long as your method frame is alive. It will, however, become eligible for GC after your method returns/exits.

The only exception, where the object created in the method is not GCed, is if you pass its references somewhere else. In this case, object still has something referring to it, and therefore, it will be maintained by the heap, and it will not be GCed.

<sup>Note, that you don't control when GC runs, object may be destroyed immediately after method returns, or after some [usually - little] while; however, in either case, you are losing the reference to it after method returns, which means, that it will eventually be GCed.</sup>

答案2

得分: 1

>我的问题是之前的狗对象会发生什么?它们还在堆内存中吗,只是没有东西可以检索它?

它们可能会在一段时间内保留,直到垃圾回收器启动或JVM退出。

void create(String name) {

    // 创建一个狗对象
    // 狗对象在方法中可用
    Dog dog = new Dog(name);
    
    return;
    // 狗对象不可访问==&gt; 垃圾回收器可以回收它。
}
英文:

>My question is what happens to the previous dog objects? Are they still in heap memory but there's nothing to retrieve it?

They may stay around for a little time, until the Garbage Collector kicks in or the JVM exits.

void create(String name) {

    // Create a dog
    //dog is available in the method
    Dog dog = new Dog(name);
    
    return;
    //dog is not reachable==&gt; The GC can collect it.
}

huangapple
  • 本文由 发表于 2020年9月21日 00:49:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63981424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定