孤立岛屿上的垃圾回收

huangapple go评论50阅读模式
英文:

Garbage Collection on Island of Isolation

问题

根据我的理解,任何没有引用的对象都可以进行垃圾回收,即在收集垃圾时应该调用类的 finalize 方法。我有下面的一个类和一个问题,为什么在主类中没有引用的线程对象在垃圾回收时不会调用 finalize 方法,如果调用了的话。

package com;

class Customer {
    int amount = 10000;

    synchronized void withdraw(int amount) {
        System.out.println("going to withdraw...");

        if (this.amount < amount) {
            System.out.println("Less balance; waiting for deposit...");
            try {
                wait(10000);
            } catch (Exception e) {
            }
        }
        this.amount -= amount;
        System.out.println("withdraw completed...");
    }

    synchronized void deposit(int amount) {
        System.out.println("going to deposit...");
        this.amount += amount;
        System.out.println("deposit completed... ");
        notify();
    }

}

class Test {
    public static void main(String args[]) throws InstantiationException, IllegalAccessException {
        final Customer c = new Customer();

        //孤立线程的示例
        new Thread() {
            public void run() {
                c.withdraw(15000);
            }
        }.start();

        //孤立另一个线程的示例
        new Thread() {
            public void run() {
                c.deposit(10000);
            }
        }.start();

        //手动尝试垃圾回收。
        System.gc();
    }

    //调用 finalize() 方法以检查是否被垃圾回收
    protected void finalize() throws Throwable {
        System.out.println("Finalize method called");
    }
}

编辑:new Thread() {//blah blah }.start(); 是一个在堆上创建的非引用对象,即它没有栈引用。 理论上,任何没有栈引用的对象都有资格进行垃圾回收,事实上也适用于垃圾回收。由于它们没有栈引用,它们也被认为是孤立的对象。

如果我对这方面的理解有误,请告诉我。感谢您的观点,如果我的思考有矛盾之处,请分享。

英文:

According to my understanding any object without a reference is eligible for garbage collection i.e The class's finalize method should be called while collecting the garbage. I have a class below and a question why the thread objects with no reference in the main class are not calling finalize Method while garbage collection if it did.

package com;
class Customer {
int amount = 10000;
synchronized void withdraw(int amount) {
System.out.println(&quot;going to withdraw...&quot;);
if (this.amount &lt; amount) {
System.out.println(&quot;Less balance; waiting for deposit...&quot;);
try {
wait(10000);
} catch (Exception e) {
}
}
this.amount -= amount;
System.out.println(&quot;withdraw completed...&quot;);
}
synchronized void deposit(int amount) {
System.out.println(&quot;going to deposit...&quot;);
this.amount += amount;
System.out.println(&quot;deposit completed... &quot;);
notify();
}
}
class Test {
public static void main(String args[]) throws InstantiationException, IllegalAccessException {
final Customer c = new Customer();
//Island of Isolating a Thread
new Thread() {
public void run() {
//				System.out.println(&quot;withdraw thread &quot;);
c.withdraw(15000);
}
}.start();
//Island of Isolating another Thread
new Thread() {
public void run() {
//				System.out.println(&quot;deposit thread &quot;);
c.deposit(10000);
}
}.start();
//attempting to gc manually.
System.gc();
}
//Calling a finialize() method to check whether it is garbage collected or not
protected void finalize() throws Throwable {
System.out.println(&quot;Finalize method called&quot;);
}
}

Edit: new Thread() {//blah blah }.start(); is an non-referenced object that gets created heap. i.e It has no stack reference. In theory, any non-Stack referenced object is eligible for garbage collection which in fact applicable to garbage collection. Since they are not Stack referenced, Also It is also considered as Island of Isolation.

Wondering if my understanding is wrong in this regard. Thanks. Please share your views if my thinking is contradictory.

答案1

得分: 2

为什么在主类中没有引用的线程对象不调用finalize方法?

垃圾收集器将活动线程视为存活的,即使没有其他引用指向这些线程。

换句话说,垃圾回收不会突然终止正在运行的代码。如果这样做的话,将会带来很大的麻烦。

此外,您只在一个从未实例化的类上实现了finalize方法。

英文:

> why the thread objects with no reference in the main class are not calling finalize

The garbage collector considers active threads as live, even if nothing else references those threads.

In other words, garbage collection does not make running code suddenly terminate. It would be a major headache if it did.

On top of that, you've only implemented finalize on a class which you never instantiate.

huangapple
  • 本文由 发表于 2020年8月10日 08:31:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63332667.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定