每天一道面试题:对象引用及垃圾回收

发布时间 2023-09-01 17:07:45作者: 一只眠羊。

先来看题目

 (多选)下面哪些描述是正确的:()【文章末尾有答案】

 1 public class Test {
 2     public static class A {
 3         private B ref;
 4         public void setB(B b) {
 5             ref = b;
 6         }
 7     }
 8     public static Class B {
 9         private A ref;
10         public void setA(A a) {
11             ref = a;
12         }
13     }
14     public static void main(String args[]) {
15 16         start();
17     ….
18     }
19     public static void start() { 
20         A a = new A();
21         B b = new B();
22         a.setB(b);
23         b = null; //
24         a = null;
25 26     }
27 }

A : b = null执行后b可以被垃圾回收

B : a = null执行后b可以被垃圾回收

C : a = null执行后a可以被垃圾回收

D : a,b必须在整个程序结束后才能被垃圾回收

E : 类A和类B在设计上有循环引用,会导致内存泄露

F : a, b 必须在start方法执行完毕才能被垃圾回收

------------------------------------------------------------------------------------------------------------?我是分隔线?--------------------------------------------------------------------------------------------------------------------

分析