469,623 Members | 1,038 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

strange __del__ behavior

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

Jul 18 '05 #1
0 1029

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Kepes Krisztian | last post: by
6 posts views Thread by Peter Abel | last post: by
13 posts views Thread by Emmanuel | last post: by
2 posts views Thread by David MacQuigg | last post: by
1 post views Thread by Hector Villafuerte | last post: by
1 post views Thread by Erwan Adam | last post: by
2 posts views Thread by Spes | last post: by
6 posts views Thread by George Sakkis | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.