3 ms·
One reason is that reference counting, without some other backup reclamation mechanism, isn't guaranteed to collect all the garbage. For example: let's say yo
by rst 15y ago
One reason is that reference counting, without some other backup reclamation mechanism, isn't guaranteed to collect all the garbage. For example: let's say you have two DOM nodes, A and B, where B is A's firstChild and A is B's parentNode, with both attributes implemented as references in the obvious way. Even if all other references to these objects disappear, making them both inaccessible, the reference from child to parentNode will keep the parent's reference count from going to zero until the child node gets reclaimed --- but the child node can't be reclaimed until the parent is gone, because the firstChild reference in the parent keeps _its_ reference count from going to zero. So neither object can be reclaimed first (or at all), and the storage for both leaks.
(The same logic applies to longer "cycles" of references --- if A refers to B refers to C refers to D refers to A, then none of the objects in the cycle can be collected first, and they all stick around.)
In CPython, by the way, there is a separate garbage collector, which runs periodically to detect and mop up cyclic structures. Other implementations --- Jython and PyPy --- don't use reference counting to begin with.
- barrkel 15y agoAnd if the values being garbage collected have no pointers in them, or the language makes cycles impossible (think functional immutable) the refcounting is all you need. It's still bona fide GC. (This is a point of definition independent of the subject at hand. Refcounting simply is a form of GC. Imperfect for most languages and many datatypes but GC all the same.)