Cycle detection and object memory usage?

John Nagle nagle at animats.com
Sun May 20 23:45:49 EDT 2007


Jim Kleckner wrote:
> cycles:
> 
> I understand from the documentation that types with a finalizer method 
> that participate in cycles can't be collected.
> 
> What is the best way to go about finding these cycles?
> Googling gives a variety of methods none of which seem terribly 
> mainstream for such a common problem.

    Put the Python garbage collector into debug mode and find out
what it reports as non-collectable.

    Use weak pointers where appropriate to avoid cycles.
The general idea is that pointers back towards the root
of a tree structure should be weak pointers, which will
result in a cycle-free structure.  I put that in BeautifulSoup,
and it works quite well.

				John Nagle




More information about the Python-list mailing list