1

i3 = null;以下に示すクラスで実行すると、4つのオブジェクトがガベージコレクションの対象になります。この答えを得た方法を説明するコメントを追加しました。私の推論は正しいですか?

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");
    }    
}
4

2 に答える 2

12

プログラムの手順は次のとおりです。

    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

したがって、最後の図は、ガベージコレクションの準備ができているオブジェクトは2つだけであると結論付けています。はっきりしているといいのですが。オブジェクト名は、オブジェクトへの参照として表示できます。

編集:

BalusCが言ったように、ロングウェイト=1200Lも対象です。したがって、i1とi3のそれぞれにさらに2つのオブジェクトが適格であるか、ガベージコレクションです。したがって、4つのオブジェクトすべてで、適格またはガベージコレクションが可能です。

于 2011-11-25T16:49:09.183 に答える
1

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.

于 2011-11-25T17:21:27.343 に答える