“最终确定的”结果会发生什么?如果我再次使用它的对象? [英] What happens to a "finalized" object if I make it available again?

查看:88
本文介绍了“最终确定的”结果会发生什么?如果我再次使用它的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我试着让一个最终化对象再次可用。我知道(来自 oracle docs finalize()不会再被调用。但是如果它变得无法访问,它会发生什么?

code> public class Solution {
static ThreadGroup stg = null;
static解决方案ss = null;

protected void finalize()throws Throwable {
System.out.println(finalized);
System.out.println(this:+ this);
ss = this;


$ b public static void main(String [] args){
解决方案s = new Solution();
s = null;
System.gc();
尝试{
Thread.sleep(1000);
} catch(InterruptedException e){
// TODO自动生成的catch块
e.printStackTrace();
}

System.out.println(ss);
ss = null; // 我的问题。现在会发生什么? Solution @ 7f5f5897刚刚变得无法访问。它会被GCed没有finalize()被调用吗? (看起来像..)。如果是的话,那么我怎么才能知道它到底什么时候才有资格使用gc(请不要告诉我看源代码== nullcheck:P)
System.gc();
尝试{
Thread.sleep(1000);
} catch(InterruptedException e){
// TODO自动生成的catch块
e.printStackTrace();
}
}

}



O / P:

 已完成
这个:Solution @ 7f5f5897 // print in finalize()
解决方案@ 7f5f5897 //打印在main()中


解决方案

是的,一旦它第二次变得无法访问,它将被GC'ed,除了 finalize 将不会被再次调用。

您可以用 WeakReference 来确认。

 静态WeakReference<解决方案> REF; 

public static void main(String [] args){
解决方案s = new Solution();

s = null;
System.gc();
尝试{
Thread.sleep(1000);
} catch(InterruptedException e){
// TODO自动生成的catch块
e.printStackTrace();
}

System.out.println(ss);
ref = new WeakReference< Jackson>(ss);
ss = null; // 我的问题。现在会发生什么? Solution @ 7f5f5897刚刚变得无法访问。它会被GCed没有finalize()被称为
//吗? (看起来像..)。如果是的话,那么我怎么才能知道它到底什么时候才有资格获得gc(请不要告诉我
//查看源代码== nullcheck:P)
System。通过out.println(ref.get()); //(希望)打印对象
System.gc();
尝试{
Thread.sleep(1000);
} catch(InterruptedException e){
// TODO自动生成的catch块
e.printStackTrace();
}
System.out.println(ref.get()); // print null
}

所以 WeakReferenc 被清除,表明该对象已被GC化。 (从技术上讲,当对象变得弱到达时, WeakReference 被清除,但是在这种情况下,因为你不能让它到达,它会被GC处理。)

Well, I tried making a finalized object available again. I know (from oracle docs) that finalize() will not be called again on it. But what happens to it if it becomes unreachable?. When will it be GCed?.

Code :

public class Solution {
static ThreadGroup stg = null;
static Solution ss = null;

protected void finalize() throws Throwable {
    System.out.println("finalized");
    System.out.println("this : " + this);
    ss = this;

}

public static void main(String[] args) {
    Solution s = new Solution();
    s = null;
    System.gc();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println(ss);
    ss = null;             // My question. What happens now?. Solution@7f5f5897 has just become unreachable. Will it be GCed without "finalize()" being called on it? (looks like that..). If yes, then how can I find out when exactly has it become eligible for gc (Please don't tell me to look at source code "==null" check :P)
    System.gc();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

O/P :

finalized
this : Solution@7f5f5897   // printed in finalize()
Solution@7f5f5897  // printed in main()

解决方案

Yes, it will be GC'ed once it becomes unreachable the second time, except finalize will not be called on it again.

You can confirm this with a WeakReference.

static WeakReference<Solution> ref;

public static void main(String[] args) {
    Solution s = new Solution();

    s = null;
    System.gc();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println(ss);
    ref = new WeakReference<Jackson>(ss);
    ss = null; // My question. What happens now?. Solution@7f5f5897 has just become unreachable. Will it be GCed without "finalize()" being called
               // on it? (looks like that..). If yes, then how can I find out when exactly has it become eligible for gc (Please don't tell me to
               // look at source code "==null" check :P)
    System.out.println(ref.get()); // (hopefully) prints object
    System.gc();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println(ref.get()); // prints null
}

So the WeakReferenc is cleared, showing that the object was GC'ed. (Technically, the WeakReference is cleared when the object becomes weakly reachable. But in this case, since you can't then make it reachable, it will get GC'ed.)

这篇关于“最终确定的”结果会发生什么?如果我再次使用它的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆