.NET Does removing a parent class turn children into garbage?

Suppose you have a Collection<B> and you are about to delete the item. Instance B refers to instance A and refers to instance C , as shown in the first figure:

Figure a

Now, since there was a link pointing to B, there is no doubt that the object is “deleted” or the garbage collector. It is simply removed from the collection, like this, right?

Figure b

Now let's create a Collection<A> with the same hierarchy of links as before, and let me delete the instance of A.

Figure c

If there is no other reference to A , it is not only deleted from the collection, but also marked as garbage. I'm right? What about B and C ? Do they also become garbage if there are no links other than B referencing an instance of C ?

This is a simplification of what I am facing. I want to remove an instance of A from the collection and want to make sure that B and C go with it. At that moment, when A no longer in my collection, all the “children” who are still alive are a memory leak for me.

When I look at these photos that I took, this seems like too silly a question. But my situation is a little less trivial . It looks something like this:

figure 4

  • In the figure, the model layer is yellow, the ViewModel layer is green
  • Class 'ViewModel' refers to its A Model
  • A Model has a collection of instances of B Model ( B is a kind of child for A both Model layers and ViewModel)
  • each B Model "knows its parent" - refers to its parent instance "Model"
  • Returning to the VM layer, “ViewModel” contains a collection of “B ViemModel” items
  • like any good ViewModel model, B ViewModel refers to model B

I have a collection of these instances of A ViewModel . When I delete one, I need everything else to go with it. Provided that there is no other “external link” to any of the instances involved (basically, there are no other arrows pointing from outside the image), will the remote instance of “ViewModel” accept all children with it? If so, are there any “mistakes” that could mislead this simplification? And if I'm completely wrong, why? :)

Thanks for reading this far!

+6
source share
1 answer

If there is no other “external link” for any of the instances involved (basically, no other arrow pointing to the input from outside the image border), does the “ViewModel” instance delete all children with it?

Yes, provided that your child does not have references to your code, he will have the right to collect garbage and ultimately must be collected.

If there is no other reference to A, not only is it removed from the collection, it is marked as garbage. I'm right?

This is actually not the case. GC does not "track garbage" - instead, it checks all references to objects from the current executable code and exits to find links that are currently "alive". Everything that remains at this stage is not living, and then becomes available for collection. If the only way to reach “B” or “C” on your chart is through this instance of “A” and you remove “A” from the collection, all of them will be eligible for GC and will be found in the next corresponding GC collection.

+10
source

Source: https://habr.com/ru/post/957395/


All Articles