#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.

930-001

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: