strange __del__ behavior

John Hunter jdhunter at ace.bsd.uchicago.edu
Wed Jun 9 12:14:54 EDT 2004


I have a class that uses some extension code I have written and I am
trying to track down some memory leaks, which I presume to be in my
extension code.  The class is question is in a python module, not
extension code.  I notice some strange behavior; perhaps a guru can
give me a pointer about what this may mean.

If I define the __del__ method in the class C

  class C:
    ...lots of other stuff...
    def __del__(self):
        print 'bye bye'

It is never called (the string 'bye bye' never prints) but the size of
the memory leak increases by a factor of 5.  If I comment out this
code the size of the memory leak goes back down.  I'll include the
code below that shows how I measure the memory leak, though it is just
a sketch because my own code is a bit more complicated

There are other parts of the code that store references to instances
of class C in lists and dictionaries, but only in standard python data
types (ie none of my extension code is holding a ref to a C instance).
I am surprised that the del function is *never* called during a run,
but am even more surprised by the fact that the memory leak increases
dramatically by defining a function that is never called!

Enlightenment please!
JDH

Measurement code and version info below:

import os, sys, time


def report_memory(i):
    pid = os.getpid()
    a2 = os.popen('ps -p %d -o rss,sz' % pid).readlines()
    print i, '  ', a2[1],
    return int(a2[1].split()[0])


def update(i):

    # this function creates a C instance and stores refs to it in
    # other python data structures, eg dicts
    c = create_a_C()  
    return report_memory(i)


N = 100
for i in range(N):
   val = update(i)
   if i==1: start = val

end = val
print 'Average memory consumed per loop: %1.4f\n' % ((end-start)/float(N))


hunter:~/python/projects/matplotlib_support/test> python
Python 2.3.2 (#1, Oct 13 2003, 11:33:15)
[GCC 3.3.1] on linux2




More information about the Python-list mailing list