references again

Thomas Thiele thiele at muc.das-werk.de
Mon Jul 10 14:13:07 EDT 2000


Hallo. My problem is still unsolved. 

I don't understand the following behaviour:

----------------------------------------
#! /usr/local/python/bin/python

import string, pickle, sys

class X:
	def __init__(self, s):
		self.s = s

x1 = X("jana")
print sys.getrefcount(x1.s)

#Problem 1:
# that prints 5 why? I expected 2! 
# (only one ref from x1.s plus temp. ref from 
# sys.getrefcount)

S = x1.s
print sys.getrefcount(x1.s)   

#prints 6, ok one reference more

str = pickle.dumps(x1)
x2 = pickle.loads(str)
print sys.getrefcount(x1.s)   

#Problem 2:
#prints 7, why? Is "jana" still used?

x1 = x2

print sys.getrefcount(x1.s)   

#prints 6, ok one reference less


#But now the most difficult problem 3:

one , two, three , four, five = 65,65,65,65,65
while(1):
	#creating different string of constant size
	str = "%c%c%c%c%c" % (one, two, three , four, five) 
	one = one + 1
	if one > 90:
		one = 65
		two = two + 1
		if two > 90:
			two = 65
			three = three + 1
			if three > 90:
				three = 65
				four = four + 1	
				if four > 90:
					four = 65
					five = five + 1	
					if five > 65:
						one , two, three , four, five = 65,65,65,65,65	
	x = X(str)
	str = pickle.dumps(x)
	x1 = pickle.loads(str)
	print str, sys.getrefcount(x1.s),

#sys.getrefcount() returns 4. I don't know why, but that is not the real
problem.
The Problem is that the program eats memory!!!!!
It seems that all older variants of str will be stored. But there is no
reference more to the older ones!

Any explanations?



More information about the Python-list mailing list