This content has been marked as final. Show 4 replies
b is not garbage collected because it still exists. It is just that now b contains the same data as a. The old data that was in b is presumably garbage collected because it is no longer referenced (because now b is referencing the same data as a).
Anyway, is that confusing enough for you?
Here's the same thing slightly rephrased.
When you set a = b you are not telling a to reference b, you are telling a to reference the same data as b. When you modify a or b you are changing the same data. Both arrays point to the same data, like two signposts pointing the same direction. Obviously you shouldn't need to references pointing to the same data.
Thanks for the resource, I actually stumbled upon it somehow!
But going back to the example, just so I'm clear - I'm pretty familiar how the referencing works, I did some pointer/theory stuff in college with C++, but very rusty now that I'm jumping back into it. I'll use arrows to make it more clear. The a( ) represents the container of 'a' (or the memory that was created)
a = 1
a -> a(1)
b = 2
a = b;
does a -> b( ), and a( ) is garbage collected
or, is b -> a( ), and b( ) is garbage collected
I'm assuming a -> b( )... (and b -> b( ) still). Right?
wah, that is actually pretty hard to follow, but I think if I understand it correctly that you have it right. With a = b I think you say, "stop holding onto your stuff, and start holding onto b's stuff--which b is also holding onto." If no one eles was simultaneously holding onto a's data when a started holding onto b's data, then the data gets dropped to the floor and GC'd.
Just remember that references don't point (or hold) to eachother, they always point to data, and I think you will be fine.