[英]Garbage Collection, .NET, C#
我有三個對象A,B和C。C是A和B的子對象。C引用了A和B。A和B引用了C。我知道當從A和B都引用C時,刪除后,C變成垃圾,將被GC收集。
public class A { public C Cr { get; set; } }
public class B { public C Cr { get; set; } }
public class C
{
public A Ar { get; set; }
public B Br { get; set; }
}
class Program
{
static void Main(string[] args)
{
var oa = new A();
var ob = new B();
var oc = new C();
oc.Ar = oa;
oc.Br = ob;
oa.Cr = oc;
ob.Cr = oc;
// Some operation
oa.Cr = null;
ob.Cr = null;
//is the following code required?
// if yes, what is the benefit?
oc.Ar = null;
oc.Br = null;
}
謝謝
內存
如果沒有對C的實時引用,那么它就有資格進行垃圾回收。 這並不意味着會立即將其收集為垃圾。
如果該對象即將成為垃圾回收對象,則無需將引用設置為null。
請注意,如果沒有對A,B或C的“根”引用,則即使它們繼續相互引用,它們也都可以進行垃圾回收。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.