OCPJP 考试的垃圾收集模拟

发布于 2024-12-17 22:33:49 字数 1451 浏览 2 评论 0原文

当在下面所示的类中执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁把谁当真 2024-12-24 22:33:49

这些是您的程序的步骤:

    Icelandic i1 = new Icelandic();
    Icelandic i2 = new Icelandic();
    Icelandic i3 = new Icelandic();

在此处输入图像描述

    i3 = i1;
    i1 = i2;

在此处输入图像描述

    i2 = null;
    i3 = null;

在此处输入图像描述

所以最后一个图得出的结论是只有 2 个对象准备好扔垃圾了 收藏。
我希望我说清楚了。您可以将对象名称视为对对象的引用。

编辑:

正如BalusC所说,长重量= 1200L也是对象。因此,i1 和 i3 各有 2 个对象符合资格或垃圾回收。所以这4个对象都符合垃圾回收条件。

These are steps of your program:

    Icelandic i1 = new Icelandic();
    Icelandic i2 = new Icelandic();
    Icelandic i3 = new Icelandic();

enter image description here

    i3 = i1;
    i1 = i2;

enter image description here

    i2 = null;
    i3 = null;

enter image description here

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.

意犹 2024-12-24 22:33:49

作为一个非常简单的经验法则,如果程序的行为不会改变,如果对象的所有字段都被复制到局部变量(优化程序转换)并且所有对该对象的引用被设置为 null。

引用“Java VM 规范”

12.6.1 实现终结
每个对象都可以由两个属性来表征:它可能是可达的、终结器可达的或不可达的,也可能是未最终化的、可最终化的或已最终化的。

可到达对象是可以在任何潜在的持续计算中从任何活动线程访问的任何对象。可以设计程序的优化转换,将可到达的对象数量减少到比天真地认为可到达的对象数量少。例如,编译器或代码生成器可能会选择将不再使用的变量或参数设置为 null,以使此类对象的存储空间可以更快地回收。

讨论

如果对象字段中的值存储在寄存器中,则会出现另一个示例。然后,程序可以访问寄存器而不是对象,并且永远不会再次访问该对象。这意味着该对象是垃圾。

因此,在您的情况下,由于对任何 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'

12.6.1 Implementing Finalization
Every object can be characterized by two attributes: it may be reachable, finalizer-reachable, or unreachable, and it may also be unfinalized, finalizable, or finalized.

A reachable object is any object that can be accessed in any potential continuing computation from any live thread. Optimizing transformations of a program can be designed that reduce the number of objects that are reachable to be less than those which would naively be considered reachable. For example, a compiler or code generator may choose to set a variable or parameter that will no longer be used to null to cause the storage for such an object to be potentially reclaimable sooner.

Discussion

Another example of this occurs if the values in an object's fields are stored in registers. The program may then access the registers instead of the object, and never access the object again. This would imply that the object is garbage.

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 dereferences i1 to i3, an optimising compiler is at liberty to elide everything after i3 = new Icelandic() as a no-op and collect all six objects immediately.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文