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:

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?

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

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:

- 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!
source share