Cycle detection and object memory usage?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon May 21 03:55:40 EDT 2007


En Sun, 20 May 2007 23:54:15 -0300, Jim Kleckner <jek-gmane at kleckner.net>  
escribió:

> I understand from the documentation that types with a finalizer method
> that participate in cycles can't be collected.

Yes; older Python versions could not manage any kind of cycles, now only  
objects with __del__ cause problems.
You can explicitely break the cycle (removing the reference, ensuring that  
some finalization method is always called, maybe using try/finally) or you  
may use weak references (by example, in a tree-like structure, a node  
might hold a weak reference to its parent).

> 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.

Avoid them in the first place :)
Use the gc module: after a call to gc.collect(), see if something remains  
in gc.garbage

> Object memory usage:
>
> Has anyone written a function to sweep out an object to discover how
> much memory it and all the objects it references is using?  This would
> be great for performance tuning.

A rough estimate may be the object's pickle size. But it's hard to measure  
precisely; by example, strings are immutable and you may have thousands of  
shared references to the same string, and they require just a few bytes  
each.

-- 
Gabriel Genellina




More information about the Python-list mailing list