销毁对象

时间:2019-10-08 03:20:13

标签: java garbage-collection

最近,在一次采访中有人问我一个问题:

  

有20个物体,需要消灭7个。编写仅使用垃圾收集器销毁7个对象的算法。

我习惯了使用System.gc()破坏对象的传统方式。

public class TestGarbage {
    public static void main(String[] args) throws InterruptedException {

        for (int i = 0; i < 20; i++) {
            TestGarbage ti = new TestGarbage();
            // System.out.println(ti);
        }

        for (int i = 0; i < 7; i++) {
            // Nullifying the reference variable
            TestGarbage ti = null;

            // requesting JVM for running Garbage Collector
            System.gc();
            // Runtime.getRuntime().gc();
        }

    }

    @Override
    // finalize method is called on object once
    // before garbage collecting it
    protected void finalize() throws Throwable {
        System.out.println("Garbage collector called");
        System.out.println("Object garbage collected : " + this);
    }
}

public class TestGarbage {
    public static void main(String[] args) throws InterruptedException {

        for (int i = 0; i < 20; i++) {
            TestGarbage ti = new TestGarbage();
            // System.out.println(ti);
        }

        for (int i = 0; i < 7; i++) {
            // Nullifying the reference variable
            TestGarbage ti = null;

            // requesting JVM for running Garbage Collector
            System.gc();
            // Runtime.getRuntime().gc();
        }

    }

    @Override
    // finalize method is called on object once
    // before garbage collecting it
    protected void finalize() throws Throwable {
        System.out.println("Garbage collector called");
        System.out.println("Object garbage collected : " + this);
    }
}

但是,面试官一点都不满意,他问我不能保证上述方法一定会运行垃圾收集器。

那么,如何解决呢?我的理解是,在Java 8或更高版本之后,程序员无需担心破坏不使用的对象。垃圾收集器会处理它。

2 个答案:

答案 0 :(得分:0)

因此,在您输入之后,我做了一些更改:

public class TestGarbage {
    public TestGarbage(int i) {
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args) throws InterruptedException {

        TestGarbage[] testGarbage = new TestGarbage[20];

        for (int i = 0; i < 20; i++) {

            testGarbage[i] = new TestGarbage(i);
            //System.out.println(testGarbage[i]);
        }

        for (int i = 0; i < 7; i++) {
            // Nullifying the reference variable
            testGarbage[i] = null;

            // requesting JVM for running Garbage Collector
            System.gc();
            // Runtime.getRuntime().gc();
        }

    }

    @Override
    // finalize method is called on object once
    // before garbage collecting it
    protected void finalize() throws Throwable {
        System.out.println("Garbage collector called");
        System.out.println("Object garbage collected : " + this);
    }
}

但是,有时垃圾收集器在输出中被调用7次-

Garbage collector called
Object garbage collected : TestGarbage@c7b3681
Garbage collector called
Object garbage collected : TestGarbage@2bcc97e
Garbage collector called
Object garbage collected : TestGarbage@6835df1
Garbage collector called
Object garbage collected : TestGarbage@4381c345
Garbage collector called
Object garbage collected : TestGarbage@169b2a8a
Garbage collector called
Object garbage collected : TestGarbage@7208e61e
Garbage collector called
Object garbage collected : TestGarbage@6ef8517b

有时只调用两次-

垃圾收集器称为 收集对象垃圾:TestGarbage @ c7b3681 垃圾收集器叫 收集的对象垃圾:TestGarbage @ 2bcc97e

答案 1 :(得分:0)

您拥有终结器方法这一事实将使事情变得混乱。当对象具有终结器并有资格进行收集时,GC会将其添加到终结器列表中。该列表由单独的JVM线程处理,这意味着终结器不能保证在任何时候(甚至在程序退出之前)都运行。

因此,每个对象需要两个收集周期的 minimum (最小)。第一个将其添加到终结器列表中,并且假设终结器在下一个GC之前已得到处理,则将在下一次收集。但是,如果有的话,可能还要收集更多的GC周期。

终结器从来都不是Java的强大功能,Object.finalize()方法在JDK 9中已弃用。