What Is Reference Counting?
Reference counting is a memory-management technique that keeps a count of how many references point to each object, incrementing on new references and decrementing when they go away. When the count reaches zero the object is freed immediately. It reclaims memory promptly but cannot collect cycles of objects that reference each other.
Why it matters
Reference counting gives deterministic cleanup without a separate collection pass, which suits resources that must be released promptly. Its blind spot is reference cycles, which is why weak references exist to break them.
Related guides
What Is a Weak Reference?A weak reference points to an object without keeping it alive, so the object can still be collected and the r…
What Is Ownership in Memory Management?Ownership is a model where each resource has a single owning variable responsible for freeing it, making clea…
What Is Mark and Sweep?Mark and sweep is a garbage collection algorithm that marks all reachable objects, then sweeps away the unmar…