Collaborations and Emergent Properties

Let’s consider the requirements for our reference finder now. In particular, what is exactly its purpose? It turns out that it is connected to the garbage collection services provided by the virtual machine.

As we discussed before, objects that are referenced, directly or indirectly, from objects like `Smalltalk` are considered not garbage. This also means that objects that cannot be found after traversing the image starting at `Smalltalk` are considered garbage, and thus the space they take can be reclaimed by the garbage collector.

What does it mean, exactly, that objects cannot be found after traversing the image starting at `Smalltalk`? Perhaps the part about traversing the image is clear already: anything that is known by `Smalltalk` is not garbage, anything that is known by objects known by `Smalltalk` is not garbage, and so on.

But what are we referring to when we say that objects cannot be found by this traversal? From a pragmatic point of view, we may consider that since we cannot send messages to garbage objects, it is safe for them to be physically removed from the image. But there is a deeper, more subtle implication at play here.

What makes an object garbage is that if any objects know it by name, then it is only because the referrers are also garbage. In other words, objects are garbage when they are not stored in any instance name (indexed or not) of objects known to be not garbage.

These would be so called *strong references*. However, there are objects that can refer to others by means of *weak references*. Weak references are not followed by the virtual machine as it traverses the image looking for garbage.

In practice, garbage detection works by making `Smalltalk` not garbage by definition, any then stating that instance name references from objects that are not garbage make the referenced objects inherit the property of being not garbage through the reference.

Fortunately, all of this is done by the virtual machine for us, so we do not have to deal with it explicitly. In fact, sometimes we may even rely on the garbage collector to make up space by getting rid of stuff our objects do not reference any more.

All is well and everybody is happy until, for some reason, the virtual machine refuses to let go of some object. This means that there is a reference to it, that there must be at least one instance name pointing to it from some object that is known to be not garbage.

Sometimes we remember where the reference is. But that does not always happen, especially in object systems composed of a large amount of parts. So, where is the reference to our suspicious object, and which is the object that knows our suspect by name?

It turns out that we can have Smalltalk figure this out by itself. Our objective is then to find all references to an arbitrary object starting from `Smalltalk`. In order to find an answer to that question, we will replicate the work of the virtual machine — to a point. Instead of looking at objects in terms of a Smalltalk space, we will look at them as forms and distinctions.