l = range(int(1E9))

Jon Ribbens jon+usenet at unequivocal.co.uk
Thu Apr 30 18:55:55 CEST 2015

On 2015-04-30, Cecil Westerhof <Cecil at decebal.nl> wrote:
> If I execute:
>     l = range(int(1E9)
> The python process gobbles up all the memory and is killed. The
> problem is that after this my swap is completely used, because other
> processes have swapped to it. This make those programs more slowly. Is
> there a way to circumvent Python claiming all the memory?
> By the way: this is CPython 2.7.8.

It's your operating system's job to handle processes.

If you use xrange() instead of range() then you will get an iterator
which will return each of the numbers in turn without any need to
create an enormous list of all of them.

More information about the Python-list mailing list