strange __del__ behavior

Discussion in 'Python' started by John Hunter, Jun 9, 2004.

  1. John Hunter

    John Hunter Guest

    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!

    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
    John Hunter, Jun 9, 2004
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jane Austine
    Steven Taschuk
    Jul 2, 2003
  2. Jane Austine
    Erik Max Francis
    Jul 2, 2003
  3. seth
  4. Kepes Krisztian

    Java final vs Py __del__

    Kepes Krisztian, Nov 27, 2003, in forum: Python
    Jay O'Connor
    Nov 27, 2003
  5. Jey Kottalam

    __del__ warnings apply to tp_dealloc?

    Jey Kottalam, Feb 3, 2004, in forum: Python
    Greg Chapman
    Feb 3, 2004

Share This Page