how to free the big list memory

Tim Peters tim.peters at gmail.com
Thu Apr 27 20:02:47 EDT 2006


[kyo guan]
> Python version 2.4.3
>
> >>> l=range(50*1024*100)
>
> after this code, you can see the python nearly using about 80MB.
>
> then I do this
>
> >>> del l
>
> after this, the python still using more then 60MB, Why the python don't free my
> memory?

It's that you've created 5 million integers simultaneously alive, and
each int object consumes 12 bytes.  "For speed", Python maintains an
internal free list for integer objects.  Unfortunately, that free list
is both immortal and unbounded in size.  floats also use an immortal &
unbounded free list.

> Is there any way to force the python free my memory?

Stop the process.

Short of that, do you really need a list containing 5 million
integers?  I never do ;-)  Something like

    for i in xrange(50*1024*100):  # note the "x" in "xrange"
        whatever

consumes a trivial amount of memory, because only two integers in the
range are simultaneously alive at any point, and the free list makes
reusing their space fast.



More information about the Python-list mailing list