Reference counting has one weakness. If object A refers to object B and B refers to A, each keeps the other’s count above zero, even when nothing else in the program can reach either of them. They are garbage that counting can never free. CPython solves this with a second mechanism, a cyclic garbage collector, which runs now and then, finds groups of objects that are only kept alive by each other, and frees them. This lesson shows the problem, the collector and the gc module that controls it, and where all of this memory comes from.
More lessons in Complete Python Programming