Garbage collector and circular reference

C#Garbage CollectionCircular Reference

C# Problem Overview


Consider these two classes:

public class A
{
     B b;
     public A(B b) { this.b = b; }
}

public class B
{
     A a;
     public B() { this.a =  new A(this); }
}

If I have classes designed like above, would the objects of such classes be collected by Garbage Collector (GC)?

Suppose I do this:

void f()
{
     B b = new B();
}

In this method, I create an instance of B called b, and when the method returns, b goes out of scope, and the GC should be able to collect it, but if it were to collect it, it would have to collect a first which is the member of B, and to collect a, it needs to collect b first which is the member of A. It becomes circular. So my question is : is such circular reference going to prevent GC from collecting the objects?

  • If yes, then how can we avoid this problem? How can we make sure that we don't have circular reference in our class design? Is there any tool (or compiler option) that helps us detecting circular reference?
  • If no, where and why do we use WeakReference class? What is its purpose?

C# Solutions


Solution 1 - C#

The .NET garbage collector can absolutely handle circular references. The very high level view of how the garbage collector works is ...

  • Start with locals, statics and GC pinned objects. None of these can be collected
  • Mark every object which can be reached by traversing the children of these objects
  • Collect every object which is not marked.

This allows for circular references to be collected just fine. So long as none of them are reachable from an object known to be uncollectable then the circular reference is essentially irrelevant.

Note: I realize I've left out many fun details in order to keep this answer simple and direct

Solution 2 - C#

No, This won't be a problem because GC can handle Circular References

MSDN Says

> If a group of objects contain references to each other, but none of > these object > are referenced directly or indirectly from stack or shared variables, then garbage > collection will automatically reclaim the memory.

Solution 3 - C#

Several answers already explained that circular references are not a problem.

As to the weak references - the reason to use them is caching.

When GC walks object dependency trees he ignores weak references. In other words if the only reference to an object is a weak one(s), it will be garbage collected, but if there was no garbage collection between reference creation and your attempt to use, you can still access the object.

Solution 4 - C#

No that circular reference will not affect the garbage collector, and it will be perfectly able to collect the instance of B.

The garbage collector knows that no-one can reference the instance of B after going out of scope, and consequently, no-one can use the instance of B to indirectly reference A.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNawazView Question on Stackoverflow
Solution 1 - C#JaredParView Answer on Stackoverflow
Solution 2 - C#Haris HasanView Answer on Stackoverflow
Solution 3 - C#mfeingoldView Answer on Stackoverflow
Solution 4 - C#driisView Answer on Stackoverflow