tuples allocated by formated print ?

Alexander Hoffmann alexander.hoffmann at netgenius.de
Wed Sep 8 12:58:54 EDT 2004


On Wednesday 08 September 2004 17:12, Jp Calderone wrote:
> 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.
That's what I was hoping  :-)

> > 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.
>
Ok, I see. So it's normal that there is a high number of Tuples allocated. But 
two facts tell me that there is something wrong:
 1. They contain personal data (for example concerning my example from above, 
I find a tuple like ("alex", ).

 2. I have my program running on my workstation and it's size in memory 
slightly increases over time.


-Alex





More information about the Python-list mailing list