Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.017 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'cpython': 0.05; 'over,': 0.07; 'python3': 0.07; 'way:': 0.09; 'python': 0.11; 'circumvent': 0.16; 'iterated': 0.16; 'range(0,': 0.16; 'elements': 0.16; ':-)': 0.16; 'wrote:': 0.18; '(but': 0.19; '>>>': 0.22; 'memory': 0.22; 'header:User-Agent:1': 0.23; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'needed.': 0.30; 'claiming': 0.31; 'gary': 0.31; 'produces': 0.31; 'used,': 0.33; 'could': 0.34; 'problem': 0.35; 'common': 0.35; 'problem.': 0.35; 'but': 0.35; 'there': 0.35; 'version': 0.36; 'really': 0.36; 'list': 0.37; 'list.': 0.37; '(i.e.,': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'most': 0.60; 'range': 0.61; 'more': 0.64; 'charset:windows-1252': 0.65; 'institute': 0.72; 'received:204': 0.75; 'dr.': 0.77 Date: Thu, 30 Apr 2015 10:05:44 -0700 From: Gary Herron User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: l = range(int(1E9)) References: <87k2wtvbx1.fsf@Equus.decebal.nl> In-Reply-To: <87k2wtvbx1.fsf@Equus.decebal.nl> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430413555 news.xs4all.nl 2906 [2001:888:2000:d::a6]:43041 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89646 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) -- Dr. Gary Herron Department of Computer Science DigiPen Institute of Technology (425) 895-4418