Python memory usage

Jorge Vargas jorge.vargas at gmail.com
Wed Nov 8 06:51:03 CET 2006


On 7 Nov 2006 21:42:31 -0800, placid <Bulkan at gmail.com> wrote:
> Hi All,
>
> Just wondering when i run the following code;
>
> for i in range(1000000):
>      print i
>
the problem of that is that all the memory is used by the list
returned by range which wont be freed until the for loop exits

try this

>>> import itertools
>>> for i in itertools.count(1000000):
...     print i

that uses an iterator which I believe will bring down the memory usage
but will kill your CPU :)

> the memory usage of Python spikes and when the range(..) block finishes
> execution the memory usage does not drop down. Is there a way of
> freeing this memory that range(..) allocated?
>
> I found this document but the fix seems too complicated.
>
> http://www.python.org/pycon/2005/papers/79/python-memory.pdf
>
> Cheers
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list