OCPJP 考试的垃圾收集模拟
当在下面所示的类中执行 i3 = null;
时,有四个对象符合垃圾回收条件。我添加了评论来解释我如何得到这个答案。我的推理正确吗?
public class Icelandic extends Horse{
public void makeNoise(){
System.out.println("vinny");
}
public static void main(String args[]){
/**
* 2 objects created
*/
Icelandic i1 = new Icelandic();
/**
* 2 objects created
*/
Icelandic i2 = new Icelandic();
/**
* 2 objects created
*/
Icelandic i3 = new Icelandic();
/**
* i3 is now pointing at i1, original Icelandic() referred to by i3 now
* has no reference - 2 objects now have no reference
*/
i3 = i1;
/**
* i3 is now pointing at i1, original Icelandic() referred to by i1 now
* has no reference - 2 objects now have no reference
*/
i1 = i2;
/**
* Total of six objects created, 4 objects no longer have a reference so 4
* can be garbage collected.
*
* Setting to null below doesn't make any difference to garbage collector
* as objects now do not have a reference
*/
i2 = null;
i3 = null;
}
}
interface Animal {
void makeNoise();
}
class Horse implements Animal{
Long weight = 1200L;
public void makeNoise() {
System.out.println("whinny");
}
}
Four objects are eligible for garbage collection when i3 = null;
is executed in the class shown below. I've added comments to explain how I got this answer. Is my reasoning correct?
public class Icelandic extends Horse{
public void makeNoise(){
System.out.println("vinny");
}
public static void main(String args[]){
/**
* 2 objects created
*/
Icelandic i1 = new Icelandic();
/**
* 2 objects created
*/
Icelandic i2 = new Icelandic();
/**
* 2 objects created
*/
Icelandic i3 = new Icelandic();
/**
* i3 is now pointing at i1, original Icelandic() referred to by i3 now
* has no reference - 2 objects now have no reference
*/
i3 = i1;
/**
* i3 is now pointing at i1, original Icelandic() referred to by i1 now
* has no reference - 2 objects now have no reference
*/
i1 = i2;
/**
* Total of six objects created, 4 objects no longer have a reference so 4
* can be garbage collected.
*
* Setting to null below doesn't make any difference to garbage collector
* as objects now do not have a reference
*/
i2 = null;
i3 = null;
}
}
interface Animal {
void makeNoise();
}
class Horse implements Animal{
Long weight = 1200L;
public void makeNoise() {
System.out.println("whinny");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些是您的程序的步骤:
所以最后一个图得出的结论是只有 2 个对象准备好扔垃圾了 收藏。
我希望我说清楚了。您可以将对象名称视为对对象的引用。
编辑:
正如BalusC所说,长重量= 1200L也是对象。因此,i1 和 i3 各有 2 个对象符合资格或垃圾回收。所以这4个对象都符合垃圾回收条件。
These are steps of your program:
So the last diagram concludes that only 2 objects are ready for garbage collection.
I hope I am clear. You can see object names as references to the objects.
EDIT:
As said by BalusC, Long weight = 1200L is also object. So 2 more objects each for i1 and i3 are eligible or garbage collections. So in all 4 objects are eligible or garbage collection.
作为一个非常简单的经验法则,如果程序的行为不会改变,如果对象的所有字段都被复制到局部变量(优化程序转换)并且所有对该对象的引用被设置为 null。
引用“Java VM 规范”
因此,在您的情况下,由于对任何
Icelandic
对象的引用都没有被取消引用,因此所有这些对象都可能会立即被垃圾收集。由于没有任何东西将i1
取消引用到i3
,优化编译器可以自由地删除i3 = new Irelandic()
之后的所有内容作为无操作,并且立即收集所有六个物体。As a very simple rule of thumb, an object in java can be garbage collected if the program's behaviour would not change if all the fields of the object were copied to local variables ( an optimising program transformation ) and all references to the object were set to null.
Quoting 'The Java VM Spec'
So in your case, as none of the references to any of the
Icelandic
objects are dereferenced, all of them may be garbage collected immediately . As nothing dereferencesi1
toi3
, an optimising compiler is at liberty to elide everything afteri3 = new Icelandic()
as a no-op and collect all six objects immediately.