Why this ref leak?

G

Gerhard Häring

import sys

def foo():
class C(object):
pass

foo()
print ">>", sys.gettotalrefcount()
foo()
print ">>", sys.gettotalrefcount()
foo()
print ">>", sys.gettotalrefcount()
[9779 refs]

Both Python 2.4 and 2.5 don't clean up properly here. Why is this?
Aren't classes supposed to be garbage-collected?

-- Gerhard
 
B

Bjoern Schliessmann

Gerhard said:
Both Python 2.4 and 2.5 don't clean up properly here. Why is this?
Aren't classes supposed to be garbage-collected?

Yes, but not neccessarily immediately.

BTW, where is your method sys.gettotalrefcount supposed to come
from? My CPython doesn't have it.

Regards,


Björn
 
P

Peter Otten

Gerhard said:
import sys

def foo():
class C(object):
pass

foo()
print ">>", sys.gettotalrefcount()
foo()
print ">>", sys.gettotalrefcount()
foo()
print ">>", sys.gettotalrefcount()
[9779 refs]

Both Python 2.4 and 2.5 don't clean up properly here. Why is this?
Aren't classes supposed to be garbage-collected?

The reference keeping the classes alive is probably object.__subclasses__():
2

Peter
 
T

Thomas Heller

Gerhard said:
import sys

def foo():
class C(object):
pass

foo()
print ">>", sys.gettotalrefcount()
foo()
print ">>", sys.gettotalrefcount()
foo()
print ">>", sys.gettotalrefcount()
[9779 refs]

Both Python 2.4 and 2.5 don't clean up properly here. Why is this?
Aren't classes supposed to be garbage-collected?

-- Gerhard

Replace "foo()" with "foo(); gc.collect()" and the refcounts are stable.
Tested the python 2.6 from trunk.

Thomas
 
D

Duncan Booth

Peter Otten said:
Oops, wrong guess:

1
The list of subclasses is stored using weak references: that's why you have
to call a method to create a real list. What actually stops the class just
vapourising is its __mro__ attribute which creates a cycle, but the garbage
collector clears that up as you saw.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top