你好,我试图了解WeakReference
,并且我已经做了一个小实验:
public class Slave {
public string Name { get; set; }
}
public class WeakOwner{
public List<WeakReference<Slave>> slaves = new List<WeakReference<Slave>>();
}
static void Main(string[] args) {
Slave slave1 = new Slave { Name = "ditone" };
Slave slave2 = new Slave { Name = "aditzone" };
WeakOwner owner = new WeakOwner();
owner.slaves.Add(new WeakReference<Slave>(slave1));
owner.slaves.Add(new WeakReference<Slave>(slave2));
slave1 = null;
slave2 = null;
GC.Collect();
Console.ReadLine(); // line where i stopped the debugger
}
我不明白对象owner
是否添加了两个WeakReference
-s并且我将被引用的对象设置为null,然后又将它们GC
设置为i,则没有其他引用可以保存那么为什么当我使用调试器访问owner.slaves
的项目时,我仍然发现两个Target
属性不为空?它们不应该为空吗?还有谁引用slave1
和slave2
来保持生命?