tuples allocated by formated print ?

Jp Calderone exarkun at divmod.com
Wed Sep 8 11:12:03 EDT 2004


Alexander Hoffmann wrote:
> Hello,
> 
> Recently I was debugging a memory leak in my app and finally figured out that 
> I had to manually implement destructors for some classes. 

   I'm not sure what this means.

> While debugging I wrote a method that prints the number of instances per type 
> for all objects returned by gc.get_objects. I was astonished by the fact that 
> after perfroming a few tests with my app there were about 4000 instances of 
> Tuple in memory. Looking at these instances I got the feeling that they 
> contained the values I used in formated prints like: print ("my name is %s" % 
> ("alex", ))
> Can anyone of you confirm that tuples constructed this way will be removed 
> from memory by Python automatically ?

   They will be.

> If so, can you imagine any reason for so many tuple instances to exist (given 
> the fact that besides in print (...) I do not make use of tuples in my source 
> code) ?

   You probably create a lot more tuples than you think.  For example:

     >>> class Foo(object):
     ...     def f(self):
     ...             pass
     ...

     >>> type(Foo.__mro__)
     <type 'tuple'>

   Also keep in mind that any module from the stdlib or a third party 
package that you import is creating objects of its own.  These are 
included in any results returned by the gc module.

   Jp



More information about the Python-list mailing list