如下代码垃圾回收时,弱引用wf为何没被清掉?
String str = new String("Hi");
ReferenceQueue rq = new ReferenceQueue();
WeakReference wf = new WeakReference(str, rq);
SoftReference<String> sr=new SoftReference<String>(str);
str=null;
System.gc();
System.gc();
String str2=sr.get();
String str1=(String) wf.get();
System.out.println("str1:"+str1);
System.out.println("str2:"+str2);
输出结果为:
str1:Hi
str2:Hi |
|