Lets say I have 2 objects - object A and object B. Object A references Object B and object B references Object A.
The GC doesn't pick an object and look to see if anything references it; keeping it if it does. The GC has a collection of every object that it knows is "alive". This collection starts out as all static variables, all variables on the stack, and a few other special cases. It then goes through each of those "alive" objects and sees what objects they reference. Each referenced object is itself marked as "alive" since it means that it is somehow reachable by another alive object. It repeats this process until no new objects are discovered. Anything that wasn't marked as alive is then known to be unreachable. As you can tell, since you have never inspected what any given "dead" object references, whether or not there is a circular reference isn't relevant.
See #1.
Well, in most of these cases it is actually referenced somewhere; in the case of a form, for example, you have Application.OpenForms
referencing any open forms. Similar constructs often exist for objects such as these. In rare cases with objects such as timers they are explicitly told by the GC to not be collected. Such situations are very rare and you generally don't need to worry about them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With