[Twisted-Python] help with refcounts and memleaks

Bob Ippolito bob at redivi.com
Mon Dec 26 12:31:32 EST 2005


On Dec 26, 2005, at 11:07 AM, Andrea Arcangeli wrote:

> I was just shoked today when I noticed this:
>
> -------------------
> import sys
>
> class A(object):
> 	y = None
> 	def x(self):
> 		pass
> 	def __del__(self):
> 		print 'deleted'
>
>
> a = A()
> print sys.getrefcount(a)
> if 1:
> 	a.y = a.x
> print sys.getrefcount(a)
> del a
> -------------------
>
> I understood the cross references memleaks well, like "x.y = y;  
> y.x= x;
> del x,y", but I didn't imagine that "a.y = a.x" would be enough to
> generate a memleak. "a.y = a.x" isn't referencing another structure,
> it's referencing itself only. Infact if I do this the memleak goes
> away!!

Objects that use __del__ don't participate in cyclic garbage  
collection.  You really want to write code that doesn't need it, or  
that uses weakref callbacks instead of __del__.

-bob





More information about the Twisted-Python mailing list