The following page discusses
reference counting using Microsoft's COM.
Allocation at runtime of large aggregates can readily exhaust memory resources. Our singly linked list example, which we looked at over the last several lessons, showed one scheme for handling this. In that example, the system reclaimed memory by traversing each list and disposing of each element. This model of reclamation is a form of
garbage collection and can involve a computationally expensive procedure. A different disposal scheme is
reference counting. With reference counting, each dynamically allocated object tracks its active references:
- When an object is created, its reference count is set to 1.
- Every time the object is newly referenced, a reference count is incremented.
- Every time the object loses a reference, the count is decremented.
- When the reference count becomes zero, the object's memory is disposed of.
Let us explore this further. In the next lesson, we'll look at a string class that has reference semantics for copying.