[Tutor] Memory problem

Alan Gauld alan.gauld at blueyonder.co.uk
Wed Jan 14 14:57:22 EST 2004


> > > > I assume you tried the simple del() function to mark it for
gc?
> > >
> We better clarify the role of del() here: del does not tell the
system to
> mark memory for garbage collection!  Rather, it removes a name
binding,
> and in effect decrements the reference count of an object.

Thats true of course but the fewer names left hanging around the
more likely te garbage collector kicks in.

> But the list that we've created is still accessible through our
'numbers'
> name:
> ###
> >>> numbers
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> ###

Indeed so you need to be careful if making multiple references
to the same object. Its the opposite of C where you can delete
an object thats referred to by multiple pointers and strange
things happen if you subsequently try to access the deleted
object.

> The only place I've really seen del() used is when we drop out
key/value
> pairs from a dictionary:

Or indeed any list object where we collect data, which is
what seemed to be happening in the OP. If we store data
in a list (or dictionary) for later processing, and fail
to del() the item after processing we effectively get a
Python memory leak. This it is important to remove items
from lists when we are finished with them. This is the one
place where Python puts responsibility for "memory
management" onto the programmer. Of course once the list
itself goes out of scope the items contained therein will
too, but if the list is global that won't be till the
end of the program...

> But that's about it.  del() probably won't fix a leak problem.

NO, but it helps where dynamically created data is stored
in a list.

> either using global variables, or somehow accumulating a lot of
values in
> a container and passing that container around.

Nope, just storing stuff in a container and forgetting
to del them promptly has the same effect. The container
just keeps on growing.

Alan G.




More information about the Tutor mailing list