#927 – Visualizing How Objects Are Created on the Managed Heap

Suppose that you create a couple of new objects and refer to them using referenced-typed variables, as follows:

            Dog myDog = new Dog("Kirby", 15);
            Dog yourDog = new Dog("Bowser", 3);

You now have two variables of type Dog, each referring to an instance of a Dog object.  The Dog objects exist on the managed heap.

927-001

Suppose that you now change the yourDog variable to also reference the “Kirby” dog, as follows:

            yourDog = myDog;

The managed heap still contains the two original Dog objects. But notice that the “Bowser” dog is no longer currently referenced by any reference-typed variables.  Bowser is no longer reachable from any of the reference-typed variables, but this Dog object has not yet been destructed, or its memory reclaimed.

972-002

Advertisement