Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.05; 'explicitly': 0.05; 'nested': 0.07; 'derived': 0.09; 'function:': 0.09; 'referenced': 0.09; 'references.': 0.09; 'subject:script': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'itself.': 0.14; 'thread': 0.14; 'times,': 0.14; 'closures': 0.16; 'closures,': 0.16; 'foo():': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'itself,': 0.16; 'reasonably': 0.16; 'surprising': 0.16; 'fix': 0.17; 'wrote:': 0.18; '>>>': 0.22; 'memory': 0.22; 'example': 0.22; 'saying': 0.22; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'circular': 0.24; 'issue,': 0.24; "shouldn't": 0.24; 'initial': 0.24; 'cc:2**0': 0.24; 'references': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'breaking': 0.31; 'consumption': 0.31; 'post.': 0.31; 'subject:all': 0.32; 'call.': 0.33; 'fri,': 0.33; 'comment': 0.34; 'problem': 0.35; "can't": 0.35; 'common': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'doing': 0.36; 'useful': 0.36; 'two': 0.37; 'anything': 0.39; 'does': 0.39; 'called': 0.40; 'even': 0.60; 'course.': 0.60; 'worry': 0.60; 'most': 0.60; 'break': 0.61; 'first': 0.61; 'stand': 0.64; 'temporary': 0.65; 'hours': 0.66; 'mar': 0.68; 'avoided.': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type:content-transfer-encoding; bh=DVKVPHUvueejeb8Mj0PwxBQ/UKIVqXaIFjF4f66I+YA=; b=J6X/qTqNAdPMQDAkg7uCbXEKcA6L3ksthNxkWKM8jULxaw5G9WBByUex+6nOetESea Hz+vod/DBKK9fntlYa5DKSGuuFwOZqt3AphA4ht3LeUJDcod7xVjHlhG0e/bNlWEGG4g gMyUrsRdAPDJV3/zPNnx90m7S2r+T20ZLK3FwJExLOhYHf9kIckKGZREu5PcgX7uTue+ EslZL97rOvy6tp+59txiR38CHMvBC5DxZJQ+Trpz+qWhcj2ZK78gPlj81h3QFd3HuiQ5 Vy7bN6AaW5trsEGBlF2p81F/ztK7Rx930ZV/JMOjQr33Ixwnww7X/Mt19CPF4eyqFcHW ZkkA== MIME-Version: 1.0 X-Received: by 10.68.112.164 with SMTP id ir4mr17570495pbb.153.1394148706571; Thu, 06 Mar 2014 15:31:46 -0800 (PST) In-Reply-To: <87d2hyrkf4.fsf@elektro.pacujo.net> References: <87ha7bq7ls.fsf@elektro.pacujo.net> <87d2hyrkf4.fsf@elektro.pacujo.net> Date: Fri, 7 Mar 2014 10:31:46 +1100 Subject: Re: script uses up all memory From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 91 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394148716 news.xs4all.nl 2917 [2001:888:2000:d::a6]:55394 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67963 On Fri, Mar 7, 2014 at 10:12 AM, Marko Rauhamaa wrote: > Chris Angelico : > >> I think this thread is proof that they are to be avoided. The GC >> wasn't doing its job unless explicitly called on. The true solution is >> to break the refloop; the quick fix is to call gc.collect(). I stand >> by the recommendation to put an explanatory comment against the >> collect call. > > What I'm saying is that under most circumstances you shouldn't care if > the memory consumption goes up and down. The true solution is to not do > anything about temporary memory consumption. Also, you shouldn't worry > about breaking circular references. That is also often almost impossible > to accomplish as so much modern code builds on closures, which generate > all kinds of circular references under the hood=E2=80=94for your benefit,= or > course. This isn't a temporary issue, though - see the initial post. After two hours of five-minutely checks, the computer was wedged. That's a problem to be solved. Most of what I do with closures can't create refloops, because the function isn't referenced from inside itself. You'd need something like this: >>> def foo(): x=3D1 y=3Dlambda: (x,y) return y >>> len([foo() for _ in range(1000)]) 1000 >>> gc.collect() 4000 >>> len([foo() for _ in range(1000)]) 1000 >>> gc.collect() 4000 >>> len([foo() for _ in range(1000)]) 1000 >>> gc.collect() 4000 That's repeatably creating garbage. But change the function to not return itself, and there's no loop: >>> def foo(): x=3D1 y=3Dlambda: x return y >>> gc.collect() 0 >>> len([foo() for _ in range(1000)]) 1000 >>> gc.collect() 0 >>> len([foo() for _ in range(1000)]) 1000 >>> gc.collect() 0 The only even reasonably common case that I can think of is a recursive nested function: >>> def foo(x): def y(f,x=3Dx): f() for _ in range(x): y(f,x-1) return y It's a function that returns a function that calls its argument some number of times, where the number is derived in a stupid way from the argument to the first function. The whole function is garbage, so it's not surprising that the GC has to collect it. >>> len([foo(5) for _ in range(1000)]) 1000 >>> gc.collect() 3135 >>> len([foo(5) for _ in range(1000)]) 1000 >>> gc.collect() 3135 >>> len([foo(5) for _ in range(1000)]) 1000 >>> gc.collect() 3135 Can you give a useful example of a closure that does create a refloop? ChrisA