l = range(int(1E9))

Ben Finney ben+python at benfinney.id.au
Thu Apr 30 19:20:10 CEST 2015

Jon Ribbens <jon+usenet at unequivocal.co.uk> writes:

> 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. […] Is
> > there a way to circumvent Python claiming all the memory?

You seem to be asking for a way to stop a program doing exactly what
it's written to do. I don't know what kind of answer you expect.

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

Indeed. In this case, the program is written to gobble up memory, and
the operating system kills it. To “circumvent” that behaviour surely
reveals the problem: that the operating system isn't handling processes
very well.

> > By the way: this is CPython 2.7.8.
> 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.

If you use Python 3 instead of the obsolescent Python 2, the ‘range’
callable has this sensible behaviour by default.

 \       “Corporation, n. An ingenious device for obtaining individual |
  `\       profit without individual responsibility.” —Ambrose Bierce, |
_o__)                                   _The Devil's Dictionary_, 1906 |
Ben Finney

More information about the Python-list mailing list