[Tutor] Memory Problem

Magnus Lyckå magnus@thinkware.se
Thu Jul 3 21:41:02 2003


At 15:37 2003-07-03 -0500, DORSEY_EDMUND_K@LILLY.COM wrote:
>def callFoo(self):
>     for i in range(0, 1000000000):
>         self.foo()

Ouch! The range function builds a list of integers, so you allocate
space both for the list object, and for the integer objects in the
list. Each integer object takes up four bytes, and I think each list
item (which is a pointer) takes up four bytes as well. That would
mean that you allocate 8 GB of memory when you do range(0, 1000000000).
(BTW, range(0,x) is the same as range(x).)

If you just use xrange() instead of range(), you won't create any
list, but I don't know if Python will actually release the memory
used for integers, since they are interned. (That integers are interned
means that if you use 42 in several different places in your code, it
will always use the same integer object.)

If your memory usage doesn't go down to something much, much
smaller with

def callFoo(self):
     for i in xrange(1000000000):
         self.foo()

you could try:

def callFoo(self):
     for i in xrange(1000):
         for j in xrange(1000):
             for k in xrange(1000):
                 self.foo()

Now, you will only use roughly 4kB, instead of 8GB. That's much less
memory, right? :) All integers from 0 to 999 will be created (and
interned). The same integers will be used in the three loops. No list
objects will be created, xrange will just return a new value for each
turn in the loop.

>Do the x,y,z variables in foo get deleted or does it need to be done 
>manually?

x, y, z are deleted just fine. That's not your problem.


--
Magnus Lycka (It's really Lyckå), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The Agile Programming Language