<legend id='k4jkE'><style id='k4jkE'><dir id='k4jkE'><q id='k4jkE'></q></dir></style></legend>

        <i id='k4jkE'><tr id='k4jkE'><dt id='k4jkE'><q id='k4jkE'><span id='k4jkE'><b id='k4jkE'><form id='k4jkE'><ins id='k4jkE'></ins><ul id='k4jkE'></ul><sub id='k4jkE'></sub></form><legend id='k4jkE'></legend><bdo id='k4jkE'><pre id='k4jkE'><center id='k4jkE'></center></pre></bdo></b><th id='k4jkE'></th></span></q></dt></tr></i><div id='k4jkE'><tfoot id='k4jkE'></tfoot><dl id='k4jkE'><fieldset id='k4jkE'></fieldset></dl></div>
      1. <small id='k4jkE'></small><noframes id='k4jkE'>

      2. <tfoot id='k4jkE'></tfoot>
          <bdo id='k4jkE'></bdo><ul id='k4jkE'></ul>

      3. 面试题:符合垃圾回收条件的对象

        时间:2023-07-12
          <bdo id='sNRGE'></bdo><ul id='sNRGE'></ul>
            <i id='sNRGE'><tr id='sNRGE'><dt id='sNRGE'><q id='sNRGE'><span id='sNRGE'><b id='sNRGE'><form id='sNRGE'><ins id='sNRGE'></ins><ul id='sNRGE'></ul><sub id='sNRGE'></sub></form><legend id='sNRGE'></legend><bdo id='sNRGE'><pre id='sNRGE'><center id='sNRGE'></center></pre></bdo></b><th id='sNRGE'></th></span></q></dt></tr></i><div id='sNRGE'><tfoot id='sNRGE'></tfoot><dl id='sNRGE'><fieldset id='sNRGE'></fieldset></dl></div>
              <tbody id='sNRGE'></tbody>

            <small id='sNRGE'></small><noframes id='sNRGE'>

                  <legend id='sNRGE'><style id='sNRGE'><dir id='sNRGE'><q id='sNRGE'></q></dir></style></legend>

                1. <tfoot id='sNRGE'></tfoot>
                  本文介绍了面试题:符合垃圾回收条件的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  给出以下代码:

                  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 
                      }
                  }
                  

                  问题是在 //Some other code 之前有多少对象可以进行垃圾回收.

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

                  那么正确答案是(至少那是面试官的答案): 2 - 布尔值 b 因为它是一个包装器和 a1 .

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

                  你能解释一下为什么 a2a3 没有被垃圾收集吗?

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

                  稍后

                  • 好的,我想我现在明白了.一开始有点困惑,但现在我确定面试官错了.我最初的错误是,一开始我并没有考虑到 Java 只是按值传递,所以不可能从以a2"作为参数的函数内部使 a2 为空,因为 a2 实际上是 a2 的副本.
                  • 带有布尔b的部分确实很明显.

                  感谢您的回答,之后我会发送一些面试反馈:).

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

                  推荐答案

                  假设 go 应该是 easyMethod 它是这样工作的

                  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 
                      }
                  }
                  

                  有两个对象可以进行垃圾回收(a1 和 a3).b 不是因为它只是对 null 的引用.从来没有Boolean.

                  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.

                  为了解决 //Some other code 可能是什么的愚蠢微妙之处,我将问题改写为以下内容:

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

                  预测并解释以下输出:

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

                  然后输出:

                  c:filesj>javac A.java
                  
                  c:filesj>java A
                  null
                  2
                  null
                  

                  后续的是,此时,a1 和 a3 符合 GC 条件,而 a2 则没有.

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

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

                  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.

                  这篇关于面试题:符合垃圾回收条件的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:什么触发了 Java 中的完整垃圾回收? 下一篇:在 JVM 中测量花费在 GC 上的时间

                  相关文章

                  <i id='cbooV'><tr id='cbooV'><dt id='cbooV'><q id='cbooV'><span id='cbooV'><b id='cbooV'><form id='cbooV'><ins id='cbooV'></ins><ul id='cbooV'></ul><sub id='cbooV'></sub></form><legend id='cbooV'></legend><bdo id='cbooV'><pre id='cbooV'><center id='cbooV'></center></pre></bdo></b><th id='cbooV'></th></span></q></dt></tr></i><div id='cbooV'><tfoot id='cbooV'></tfoot><dl id='cbooV'><fieldset id='cbooV'></fieldset></dl></div>
                  <tfoot id='cbooV'></tfoot>
                    <bdo id='cbooV'></bdo><ul id='cbooV'></ul>

                  <small id='cbooV'></small><noframes id='cbooV'>

                2. <legend id='cbooV'><style id='cbooV'><dir id='cbooV'><q id='cbooV'></q></dir></style></legend>