l = range(int(1E9))
gherron at digipen.edu
Thu Apr 30 19:05:44 CEST 2015
On 04/30/2015 09:06 AM, Cecil Westerhof 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.
Well, that could be considered the problem. In Python3, the range
function returns a range object which takes up almost no resources,
while in Python2 it produces a list. Both can be iterated over, so they
produce the same result in the most common use case (i.e., iteration),
but the Python3 version generates the elements only as needed.
If you really *wanted* the list (but WHY?) in Python3, do
list(range(...)), but then you get what you deserve. :-)
>>> l = range(int(1E9))
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
More information about the Python-list