#930 – Objects on the Heap Can Refer to Each Other

When you set a reference-typed variable to refer to an object, either by creating a new object, or by referencing an existing object, you reference an object on the heap.  As long as the reference exists, the object will be allowed to exist on the managed heap and not get garbage collected.

In addition to an object being referenced by a reference-typed variable, objects may be referenced by other objects.  It may also be the case that an object is only referenced by another object and not by any reference-typed variable.  These objects will be kept alive only if they can be reached by some reference.

In the example below, the dogs HankLassie and Rex will all become eligible for collection because they are not reachable by any referenced-typed variable.  Dogs Kirby and Jack will be retained, because they are reachable.