Jacob Logas
Jacob Logas

Reputation: 33

C# Tree Node Removal and Memory Management

I am working on a C# application that has a tree structure. The structure is made up of a list of Component objects and each Component can have a list of subcomponents. My question is if I remove the root of the tree (or subtree), does the garbage collector know to delete all the subcomponents and sub-subcomponents?

Upvotes: 3

Views: 302

Answers (1)

Servy
Servy

Reputation: 203844

If there is no reference to any of those noted through any rooted object, then they are eligible for collection.

Put another way, if it's possible for any code to execute that would attempt to reference that object, then it cannot be collected; if the program can determine that it's impossible for any code executed in the future to attempt to access that object, then they are eligible for collection.

That the objects are referenced by other objects that aren't accessible isn't relevant.

Upvotes: 2

Related Questions