Johann Gerell
Johann Gerell

Reputation: 25581

Will this application tree be garbage collected?

In my app composition root a large instance tree is built where parent nodes can be implicitly referenced by child nodes through for instance delegates and observers. This is a simplified view of the managed objects' relationships:

A    <---+
  B    | |
  C  - + |
  D      |
E        | <-+
  F  ----+   |
  G          |
    I -------+  

My hunch is that if I in my composition root have references to objects A and E and then set those to null, the complete object tree will be garbage collected.

Correct?

Upvotes: 2

Views: 90

Answers (1)

Petar Ivanov
Petar Ivanov

Reputation: 93040

This is correct, given that you don't have any other reachable references to nodes in the tree. This is because unreachable objects referencing each other are still eligible for garbage collection if there are no external (i.e. reachable) references to any of them.

Upvotes: 3

Related Questions