Python memory usage

William Heymann kosh at aesaeion.com
Wed Nov 8 00:55:54 EST 2006


On Tuesday 07 November 2006 22:42, placid wrote:
> Hi All,
>
> Just wondering when i run the following code;
>
> for i in range(1000000):
>      print i
>
> 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

Change range to xrange. It will run faster and use up almost no memory by 
comparison. I know the point you are getting at for releasing memory however 
in this case there is no reason to allocate the memory to begin with.

Stranegly enough on my python2.4 install (kubuntu edgy) about half the memory 
gets released as soon as the call finishes however if I run the same call 
again it only goes up to the memory usage it was before the memory was 
released. So some of the memory is returned and some is reused by python 
later.



More information about the Python-list mailing list