-2

I know that when an object has no references, the garbage collector frees the memory. But I have a doubt.

I have a class that is represented by a tree:

class MyNode
{
    string Name;
    List<MyNode> Descendants;
    MyNode Parent;
}

When I create a node that has descendants, I have the reference to the node and also the son has a reference to the node in the Parent property. Also, the node has a reference to the descendant in the list of descendants.

So an example.

Node myNode = new Node();
Node myDescendant = new Node();
myNode.Descendants.Add(myDescendant);
myDescendant.Parent = myNode;

I can delete the reference to myNode setting it to null. However, the object is still referenced because myDescendant references to it in the Parent property. The myDescendant object is not recollected because is referenced in the descendants list of the node object.

So it seems that there are a cycle that avoid the node recollection. Is this true? Perhaps I need to set to null all the properties in the node and in the descendants and clear the list of descendants?

Thanks so much.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
Álvaro García
  • 18,114
  • 30
  • 102
  • 193
  • 1
    _"I have a class that is a ... struct"_ is contradictionary, and then you leave out the `class` | `struct` keyword that could have clarified it. Also you mix `Node` and `MyNode`. – H H Jan 09 '15 at 17:58
  • 1
    *I have a class that is a tree struct:* Umm what? Seriously? Struct or Class ? – Sriram Sakthivel Jan 09 '15 at 17:59
  • 1
    @HenkHolterman If it were a `struct` in the C# sense then he couldn't set it to `null`. It is clearly a `class`. The type is representing a tree structure using the more general definition of "structure", not the C# keyword definition. I.e., it's a class that represents a tree data structure. – Servy Jan 09 '15 at 18:00
  • 2
    @Servy - the code is still incomplete and inconsistent. Don't you think the OP should clean up and clarify a little? – H H Jan 09 '15 at 18:02
  • Ok, I expressed badly, I wanted to say that I have a class that I use to create a tree. – Álvaro García Jan 09 '15 at 18:04
  • 1
    @HenkHolterman Sure he should. I'm just saying that while it's presented poorly, the question isn't actually ambiguous when looked at in context. It should still be cleaned up so that one doesn't need to spend so much effort to understand it though, yes. – Servy Jan 09 '15 at 18:07

1 Answers1

1

Objects are only collected when there are no references that are accessible from a rooted location. Rooted locations are locations that are "in scope" throughout the entire application's lifetime, such as static variables, the stack, etc.

Since these objects have a cyclical reference, if either of them is accessible through a rooted reference (a local variable, a static variable, or anything accessible through another series of references that link back to a rooted reference), then neither are eligible for garbage collection. If neither object is accessible from a rooted reference, then both objects are eligible for garbage collection.

Servy
  • 202,030
  • 26
  • 332
  • 449