[Twisted-Python] help with refcounts and memleaks

Christopher Armstrong radeex at gmail.com
Mon Dec 26 21:47:42 EST 2005


On 12/27/05, Jesus Cea <jcea at argo.es> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Wow, Andrea Arcangeli, of Linux Kernel fame. What an honor :).
>
> > +             import gc
> > +             gc.collect()
>
> Haha. I was just writing you about manual garbage collector invocations.
>
> Manual collections can be costly, nevertheless. Since the manual
> collector is only needed to free cyclic references, I guess that Twisted
> people would rather prefer a patch to break cyclic references in Twisted
> code. For example, using "__del__" methods or "weakref".
>
> You can easily see what objects and references keep the cycles alive
> using "gc.garbage" and "gc.get_referrers()". Usually a single
> "a.value=None" can do miracles :-). Weakref can be very very helpful, also.

Just some points of clarification, as I don't think Jesus has got the
whole story:

 - gc.collect() is the equivalent of what's run occassionally by
Python. So explicitly calling it is *not* required to break all
circular references. In this case, it only helped because Andrea was
accumulating objects quicker than the Python-scheduled gc.collect was
being run.
 - I really doubt we need a patch to Twisted to break any cyclic references. :)
 - gc.garbage contains what _won't_ be collected by a call to
gc.collect(). gc.garbage contains only broken cycles (e.g., those with
objects that have __del__ methods), not all cycles.
- Ever since the new gc system was enabled by default in Python, it
should never be necessary to manually break cycles unless you have
__del__ crap involved.


--
  Twisted   |  Christopher Armstrong: International Man of Twistery
   Radix    |    -- http://radix.twistedmatrix.com
            |  Release Manager, Twisted Project
  \\\V///   |    -- http://twistedmatrix.com
   |o O|    |
w----v----w-+




More information about the Twisted-Python mailing list