简体   繁体   English

对垃圾收集感到困惑

[英]Confused about garbage collection

In the following code how many objects are eligible for garbage collection after executing line 7? 在下面的代码中,执行第7行后有多少对象符合垃圾回收的条件? In my opinion 1 object that is z is eligible. 在我看来,1个z对象是合格的。 Is is right? 是对的吗?

public class Tester 
{ 
  public static void main(String[] args) 
  {

    Integer x = new Integer(3000);   
    Integer y = new Integer(4000);   
    Integer z = new Integer(5000);

    Object a = x;   
    x = y;  
    y = z;
    z = null; //line 7 
   }
}

Thank you very much. 非常感谢你。

Don't confuse a reference with an object. 不要将引用与对象混淆。 The object is the actual item that has been created, the reference is simply a name that refers to it. 对象是已创建的实际项目,引用只是引用它的名称。

You've created three objects, let's call them 3000 , 4000 and 5000 . 您已经创建了三个对象,我们称他们为300040005000 You've also set up references as follows: 您还设置了如下引用:

Ref    Object
---    ------
 x  ->  3000
 y  ->  4000
 z  ->  5000

After the assignments, you end up with: 完成作业后,您最终得到:

Ref    Object
---    ------
 a  ->  3000
 x  ->  4000
 y  ->  5000
 z

Hence none of the objects are subject to garbage collection. 因此,没有任何对象受到垃圾收集。 Every single one still has a reference to it. 每一个人都有一个参考。


By way of contrast, if you were to remove the line: 相比之下,如果您要删除该行:

Object a = x;   

then the assignments would then result in: 那么分配将导致:

Ref    Object
---    ------
        3000
 x  ->  4000
 y  ->  5000
 z

and the object we called 3000 would be eligible for garbage collection, since you no longer have any way to access it. 而我们称之为3000对象将有资格进行垃圾收集,因为您无法再访问它了。


And, as an aside, you may want to consider the fact that one of the major reasons automated garbage collection was created was to make these sorts of questions moot :-) Generally (though there are of course exceptions), you shouldn't need to worry about what objects are subject to collection. 而且,作为一个旁白,您可能想要考虑这样一个事实,即创建自动化垃圾收集的一个主要原因是使这些问题没有实际意义:-)一般(尽管当然有例外),您不应该需要担心哪些对象可以收集。

None of the objects are eligible for Garbage Collections . 没有任何对象符合垃圾收集条件

Every object has a live references. 每个对象都有一个实时引用。 z is not an Object, it's just a reference. z不是Object,它只是一个引用。 You have moved the references only. 您仅移动了引用。

x, y and z aren't objects - they are references to objects. x,y和z不是对象 - 它们是对象的引用。

Informally, any object is eligible for garbage collection when there's no way you could possibly ever access it any more. 非正式地,当你无法再访问它时,任何对象都有资格进行垃圾收集。 After running this code, x refers to the 3000 object, y refers to the 4000 object, and z refers to the 5000 object. 运行此代码后,x引用3000对象,y引用4000对象,z引用5000对象。 The 3000 object cannot be collected, because you could use it, eg System.out.println(a); 无法收集3000对象,因为您可以使用它,例如System.out.println(a); . The 4000 object cannot be collected, because you could use it through x, eg System.out.println(x); 无法收集4000对象,因为您可以通过x使用它,例如System.out.println(x); . The 5000 object cannot be collected, because you could use it through z. 无法收集5000对象,因为您可以通过z使用它。

After main returns, all of those objects are eligible for garbage collection, because you can't access them after that. main返回后,所有这些对象都有资格进行垃圾回收,因为之后您无法访问它们。

z won't be eligible for GC at line 7. It is because in line 6, y is refrencing the object which was previously refrenced by z. z将不符合第7行的GC条件。这是因为在第6行中,y正在重新呈现之前由z引用的对象。 So even thought you have made z=null , y is still refrencing the object created in the heap ie. 所以即使你认为你已经使z=null ,y仍然在重新生成在堆中创建的对象,即。 new Integer(5000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM