memory

Dirk Heise dheise at debitel.net
Thu Jan 27 22:31:57 EST 2000


Matt Cheselka <cheselka at tucana.noao.edu> schrieb im Beitrag <86q8ee$2g7k$1 at noao.edu>...
> Hi,
> 
> Are there any issues with freeing memory in python?  Say for example I do a
> 
> 	d = array.array('i')
> 	for i in range (2000):
> 	    d.append(0)
> 
> Do I need to free 'd' before I quit the program?
> 
> Cheers,
> 
> Matt Cheselka
> 

As "bjorn" said, it will be freed automatically when the
last reference to it is gone. This means there is a
trap:

a = [] # an empty list
b = [] # another one
a.append(b) # The first element of a will be the list 
  # referred to by b
print a # should be [[]]
b.append(a) # do the reverse thing to b
print b # at least PythonWin is cute enough to not run into
  # an endless recursion here and prints "[[[...]]]"
  
Now each list refers to the other; a less cute print could
run into an endless recursion. 

del a
del b

will help you nothing here. The lists form a cycle,
so the refcounts will never go down to 0 if you don't
do something about it. So they will not be disposed,
although you deleted all references to access them.

Dirk





More information about the Python-list mailing list