Memory leak: assigning function object to instance variable

echuck at mindspring.com echuck at mindspring.com
Tue Jun 6 13:03:19 EDT 2000


In article <8hh0pf$i7$1 at phys-ma.sol.co.uk>,
  "John Parkey" <jparkey at nospamrapidbs.com> wrote:
> I have registered this bug, with code fragment below, but wondered if
> anybody else was falling foul of memory leaks of this type.  We use
xmllib,
> which relies on assigning methods to instance variables for later
callbacks.
> Our recent volume testing has shown serious leakage relating to this
problem
> (I use it in other places too, so it's pretty serious for us).
Environment
> is 1.5.2 on NT, build 132.  To see the problem, run the example below
and
> watch memory usage in task manager.
>
> (Quick plug for Mark Hammond - thanks for loads of help, and
finding/fixing
> COM related leaks). I hope my week of leak hell will soon be over!
>
> class fred:
>  def __init__(self):
>   self.indirectFunc = self.theFunc
>
>  def theFunc(self):
>   return "blah"
>
> def test():
>  f = fred()
>  del f
>
> if __name__ == "__main__":
>  for x in xrange(1000):
>   test()
>
>

John,

This is not an actual bug. When you say:

        self.foo = self.someMethod

What you get is a _bound method_. e.g., bound to 'self'. I bet when you
invoke your method with apply() you don't pass self. That's because
self is already stored in the bound method. This causes a circular
reference, and as you probably already know, Python doesn't collect
those (by design).

We had this problem in Webware for Python. Our solution:

        self.foo = self.__class__.someMethod

And the apply() has to pass self. Now the circular refs are gone.

I did a performance benchmark and the second method seems to be ever so
slightly slower, but not much. In the context of everything else you
do, you won't see it.

Happy coding.

-Chuck
http://webware.sourceforge.net


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list