#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

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: