面试问题:有资格进行垃圾回收的对象 [英] Interview question: Objects eligible for garbage collection

查看:87
本文介绍了面试问题:有资格进行垃圾回收的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下代码:

  class A {
Boolean b;
easyMethod(A a){
a = null;
返回a;
}
public static void main(String [] args){
A a1 = new A();
A a2 = new A();
A a3 = new A();
a3 = a1.easyMethod(a2);
a1 = null;
//一些其他代码
}
}

是多少个对象有资格在之前进行垃圾收集//其他一些代码



然后正确答案是(至少这是面试官的答案): 2 - 布尔 b ,因为它是一个包装器, a1 a2 a3 c $ c>没有被垃圾收集?



后期编辑:




  • 我想我现在明白了。起初有点混乱,但现在我确定面试官是错的。我最初的错误是,起初我并不认为Java只是传值,所以不可能在以a2作为参数的函数内部产生a2 null,因为a2实际上是a2的副本。




  • 感谢您的回答,我会在那之后发送一些采访反馈:)。

    解决方案

    假设转到应该是 easyMethod 它可以像这样工作

      class A { 
    布尔b;
    easyMethod(A a){
    a = null; //对a2的引用已传入,但设置为空
    // a2未设置为null - 引用的此副本是!
    返回a; //返回null

    public static void main(String [] args){
    A a1 = new A(); // 1 obj
    A a2 = new A(); // 2 obj
    A a3 = new A(); // 3 obj
    a3 = a1.go(a2); // a3设为null并标记为GC - 请参阅上面的原因
    a1 = null; //到目前为止,a1和a3已被设置为null并标记为
    //其他一些代码
    }
    }

    两个对象有资格进行垃圾回收(a1和a3)。 b 不是因为它只是对null的引用。没有布尔曾经被制作过。为了避开 // 中的微妙细节, $ b

    其他一些代码可能是,我反而会将这个问题重新写入以下内容:



    定价并解释以下输出:

      class A {
    int i;
    A(int i){this.i = i; }
    public String toString(){return+ i; }
    A去(A a){
    a = null; //对a2的引用已传入,但设置为空
    // a2未设置为null - 引用的此副本是!
    返回a; //返回null

    public static void main(String [] args){
    A a1 = new A(1); // 1 obj
    A a2 = new A(2); // 2 obj
    A a3 = new A(3); // 3 obj
    a3 = a1.go(a2); // a3设为null并标记为GC - 请参阅上面的原因
    a1 = null; //到目前为止,a1和a3已被设置为空并且标记为

    test(a1);
    test(a2);
    test(a3);


    static void test(A a){
    try {System.out.println(a); }
    catch(Exception e){System.out.println((String)null); }
    }
    }

    并输出:

      c:\files\j> javac A.java 

    c:\files\j> java A
    null
    2
    null

    接下来是在那一点,a1和a3符合GC要求,而a2不符合要求。



    这个问题的教训是将对象引用传递给方法并将引用设置为null不会导致原始参考无效。这是面试官试图测试的知识。

    Give the following code:

    class A {
        Boolean b;
        A easyMethod(A a){
            a = null;
            return a;
        }
        public static void main(String [] args){
            A a1 = new A();
            A a2 = new A();
            A a3 = new A();
            a3 = a1.easyMethod(a2);
            a1 = null;
            // Some other code 
        }
    }
    

    The question is how many objects are eligible for garbage collection right before // Some other code.

    Then correct answer is (at least that's the interviewer answer): 2 - the Boolean b because it's a wrapper and a1 .

    Can you please me explain me why a2 and a3 aren't being garbage collected ?

    LATER EDIT:

    • Ok, I think I get it now. It was a bit confusing at first, but now i am sure the interviewer was wrong. My initial mistake was that at first I didn't consider that Java is pass by value only, so it's impossible to make a2 null from inside a function that take "a2" as a parameter, because that a2 is actually a copy of a2.
    • The part with the Boolean b was indeed quite obvious.

    Thanks for an answer, I will send some interview feedback after that :).

    解决方案

    Assuming go is supposed to be easyMethod it works like this

    class A {
        Boolean b;
        A easyMethod(A a){
            a = null; // the reference to a2 was passed in, but is set to null
                      // a2 is not set to null - this copy of a reference is!
            return a; // null is returned
        }
        public static void main(String [] args){
            A a1 = new A(); // 1 obj
            A a2 = new A(); // 2 obj
            A a3 = new A(); // 3 obj
            a3 = a1.go(a2); // a3 set to null and flagged for GC - see above for why
            a1 = null; // so far, a1 and a3 have been set to null and flagged
            // Some other code 
        }
    }
    

    Two objects are eligible for garbage collection (a1 and a3). b is not because it's only a reference to null. No Boolean was ever made.

    To get around the inane subtleties of what // Some other code might be, I instead posit the question be reworded into the following:

    Prdict and explain the following output:

    class A {
        int i;
        A(int i) { this.i = i; }
        public String toString() { return ""+i; }
        A go(A a){
            a = null; // the reference to a2 was passed in, but is set to null
                      // a2 is not set to null - this copy of a reference is!
            return a; // null is returned
        }
        public static void main(String [] args){
            A a1 = new A(1); // 1 obj
            A a2 = new A(2); // 2 obj
            A a3 = new A(3); // 3 obj
            a3 = a1.go(a2); // a3 set to null and flagged for GC - see above for why
            a1 = null; // so far, a1 and a3 have been set to null and flagged
    
            test(a1);
            test(a2);
            test(a3);
    
        }
        static void test(A a) {
            try { System.out.println(a); } 
            catch(Exception e) { System.out.println((String)null); }
        }
    }
    

    And output:

    c:\files\j>javac A.java
    
    c:\files\j>java A
    null
    2
    null
    

    And the followup is that at that point, a1 and a3 were eligible for GC, and a2 was not.

    The lesson from this question is that "Passing an object reference to a method and setting that reference to null does not cause the original reference to be nulled". That's the piece of knowledge the interviewer was attempting to test.

    这篇关于面试问题:有资格进行垃圾回收的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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