memory usage, temporary and otherwise

mk mrkafk at gmail.com
Thu Mar 4 06:56:17 EST 2010


Bruno Desthuilliers wrote:
> mk a écrit :
>> Obviously, don't try this on low-memory machine:
>>
>>>>> a={}
>>>>> for i in range(10000000):

> Note that in Python 2, this will build a list of 10000000 int objects.
> You may want to use xrange instead...

Huh? I was under impression that some time after 2.0 range was made to 
work "under the covers" like xrange when used in a loop? Or is it 3.0 
that does that?

> And this build yet another list of 10000000 int objects.

Well this explains much of the overhead.

> (overly simplified)
> 
> When an object is garbage-collected, the memory is not necessarily
> "returned" to the system - and the system doesn't necessarily claim it
> back neither until it _really_ needs it.
> 
> This avoid a _lot_ of possibly useless work for both the python
> interpreter (keeping already allocated memory costs less than immediatly
> returning it, just to try and allocate some more memory a couple
> instructions later) and the system (ditto - FWIW, how linux handles
> memory allocations is somewhat funny, if you ever programmed in C).

Ah! That explains a lot. Thanks to you, I have again expanded my 
knowledge of Python!

Hmm I would definitely like to read smth on how CPython handles memory 
on Python wiki. Thanks for that doc on wiki on "functions & methods" to 
you and John Posner, I'm reading it every day like a bible. ;-)


Regards,
mk




More information about the Python-list mailing list