l = range(int(1E9))

Rob Gaddi rgaddi at technologyhighland.invalid
Thu Apr 30 13:12:16 EDT 2015


On Thu, 30 Apr 2015 10:05:44 -0700, Gary Herron wrote:

> 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. :-)
> 
> Python3:
> 
>  >>> l = range(int(1E9))
>  >>> l
> range(0, 1000000000)

This also leads to a unrelated question, Cecil.  Given that you really 
are just starting to get your Python feet under you, why are you using 
Python2?  Python3 is the standard now, Python2 is really just given 
legacy support.  I'd understand if you were trying to maintain an old 
codebase with lots of legacy code that was having "problematic" 
migrations, but with the opportunity to start fresh?  Start fresh.  
You'll be happier for it.


-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.



More information about the Python-list mailing list