[Tutor] Memory problem

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Jan 14 14:20:32 EST 2004



On Tue, 13 Jan 2004, Alan Gauld wrote:

> > > I assume you tried the simple del() function to mark it for gc?
> >
> > Wether or not this actually frees up memory will depend on what OS the
> > program runs on. On some unix systems, once the memory has been
> > allocated to a process, the kernel might not try to reclaim it.
>
> This is true but regular use of del() ensures that memory growth is
> limited to the minimum needed.

Hi Alan,


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.


Concrete example:

###
>>> numbers = range(10)
>>> digits = numbers
>>> numbers
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> digits
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
###

Here, we've constructed one object --- a range of 10 numbers --- and bound
it to the name 'numbers'.  We've also bound that same object to the name
'digits'.



Ok, now let's do a del().

###
>>> del(digits)
###

It's important to see that all that del() does is drop the 'digits' name.

###
>>> digits
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'digits' is not defined
###



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]
###


The C implementation of Python uses a reference count scheme to manage
memory: when the number of names that refer to an object drops to zero,
then Python knows its safe to reclaim that memory.


(I'm not quite telling the truth: Python does have a hybrid system that
can handle recursive structures, so it's resistant to the kind of problems
that would confound a simple reference count system.)



Python programmers rarely use del() explicitly on variable names, because
names automatically drop out anyway whenever a local function exits.  If
we have something like:

###
>>> def foo():
...     bar = range(10**6)
...
>>> while 1:
...     foo()
...
###

then this code spins the computer's wheels, but at least it does not
exhaust memory.  'bar' goes out of scope when foo() exits, and then that
range of numbers gets collected since its reference count drops to zero.


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

###
>>> d = {1: 'one', 2: 'two', 3: 'three'}
>>> del d[2]
>>> d
{1: 'one', 3: 'three'}
###


But that's about it.  del() probably won't fix a leak problem.  Any
"leaking"  that's occurring in pure Python code should be the result of
either using global variables, or somehow accumulating a lot of values in
a container and passing that container around.  We really do need to see
code, and trace the flow of variables around --- from there, we can
probably pick out where the leak is occurring.



Hope this helps!




More information about the Tutor mailing list