C#中的内存泄漏问题

问题描述:


我是C#程序员.我担心的是内存泄漏.因为它在我的程序中很关键.

我知道在这种情况下:
obj1 = {parent = null,child = obj2}
obj2 = {parent = obj1,child = null}
其中obj1是全局对象,而obj2是局部对象,此代码:

Hi,
I''m a c# programmer. And the thing I''m concern about is memory leak. Because it is critical in my program.

I know in this case:
obj1={ parent=null, child=obj2 }
obj2={ parent=obj1, child=null }
where obj1 is a global object and obj2 is a local object, this code:

obj1.child = null;


导致删除obj2.

但是我怀疑以下代码是否会删除obj2和obj3?因为他们两个仍然指向对方:
obj1 = {parent = null,child = obj2}
obj2 = {父代= obj1,子代= obj3}
obj3 = {parent = obj2,child = null}


causes removing obj2.

But I''m doubtful if following code results removing obj2 and obj3 or not? because both of them still point each other:
obj1={ parent=null, child=obj2 }
obj2={ parent=obj1, child=obj3 }
obj3={ parent=obj2, child=null }

obj1.child = null;
此代码将减少obj2的引用计数.如果没有对obj2的引用,它将在GC期间将其删除.
在第二种情况下,如果没有更多对obj2和obj3的引用,则将obj1.child设置为null将导致删除两个对象:obj2和obj3,因为它们将被视为孤立的对象图.
obj1.child = null;
This code will decrease the reference count for obj2. The obj2 itself will be removed during the GC, when there are no references to it.
In the second case, if there are no more references to obj2 and obj3, setting obj1.child to null will cause removal of two objects: obj2 and obj3 because they will be considered orphaned object graph.