Reference Counts

raghu raghavan.viswanathan at wipro.com
Thu May 18 02:28:15 EDT 2006


Hi All,

I am a new user of Python and am having a bit of problem understanding
the Reference counting and memory leakage issues.

Requesting help from experienced users....

I wrote the following simple program.


#!/usr/bin/python

import sys
global a

print "Total Reference count at the start =",sys.gettotalrefcount()
a=1
print "a ref count =",sys.getrefcount(a)
b=a
print "a ref count =",sys.getrefcount(a)

del a
del b

print "Total Reference count at the end =",sys.gettotalrefcount()


I executed it. I am seeing the following.

Total Reference count at the start = 16538
a ref count = 49
a ref count = 50
Total Reference count at the end = 16540
[6416 refs]

There are a few questions that I am having on this.

(1) Why should 'a' reference count be 49 before I even made an
assignment ?
(2) The Total Reference count at the end has increased by 2 . Why ? Am
I leaking memory ?
(3) I have read somewhere that an increase in sys.gettotalrefcount() is
indicative of a memory leak ? Aint that correct ?

Thanks for the help.

Bye,
raghavan V




More information about the Python-list mailing list