Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.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.022 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'interpreter': 0.07; 'python': 0.08; '>>>>': 0.09; 'allocates': 0.09; 'be:': 0.09; 'consume': 0.09; 'question:': 0.09; 'message-----': 0.12; 'subject:python': 0.12; 'subject:file': 0.13; 'wrote:': 0.15; 'afterwards.': 0.16; 'explanation': 0.16; 'formatted)': 0.16; 'great!': 0.16; 'numpy': 0.16; 'received:192.168.1.40': 0.16; 'subject:memory': 0.16; 'holds': 0.16; '>>>': 0.16; 'figure': 0.21; 'memory': 0.21; 'header:In-Reply-To:1': 0.22; 'module,': 0.23; 'loaded': 0.25; 'string': 0.26; 'somebody': 0.28; 'objects': 0.28; 'lists': 0.29; '---': 0.30; 'subject:': 0.30; 'array': 0.30; 'arrays': 0.30; 'thanks': 0.31; 'url:library': 0.31; 'chris': 0.32; 'rather': 0.33; 'to:addr:python-list': 0.34; 'header:User- Agent:1': 0.34; 'operating': 0.34; 'sent:': 0.34; 'themselves,': 0.35; 'usage': 0.35; 'question': 0.35; 'running': 0.35; 'from:': 0.36; 'idea': 0.36; 'explain': 0.36; 'file': 0.36; 'url:python': 0.37; 'doing': 0.37; 'could': 0.37; 'using': 0.37; 'received:192': 0.38; 'url:org': 0.38; 'subject:: ': 0.38; 'think': 0.38; 'perhaps': 0.39; 'received:192.168.1': 0.39; 'should': 0.39; 'data': 0.39; 'url:docs': 0.39; 'skip:s 20': 0.39; 'to:addr:python.org': 0.39; 'help!': 0.40; 'give': 0.60; 'your': 0.60; 'easily': 0.61; 'peter': 0.62; 'back': 0.63; 'cost': 0.65; 'received:62': 0.67; 'august': 0.69; '4:26': 0.84; '7.89': 0.84; 'from:addr:t': 0.84; 'optimisation': 0.84; 'otten': 0.84; 'url:array': 0.84; 'involved.': 0.91; 'subject:cost': 0.91 Date: Tue, 02 Aug 2011 13:09:57 +0200 From: Thomas Jollans User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20110628 Thunderbird/5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: python reading file memory cost References: <000f01cc505c$74e01e80$5ea05b80$@com> <1312255370.31275.20.camel@ZTFEL> <000a01cc5103$6be8ea80$43babf80$@sinap.ac.cn> In-Reply-To: <000a01cc5103$6be8ea80$43babf80$@sinap.ac.cn> X-Enigmail-Version: 1.2 OpenPGP: id=5C8691ED Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1312283392 news.xs4all.nl 23846 [2001:888:2000:d::a6]:56564 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10717 On 02/08/11 13:00, 张彤 wrote: > Thanks Peter! Your explanation is great! > And one more question: > Why it is still keeping the memory even when I del the large array in > interactive python mode? This is an optimisation of the way the Python interpreter allocates memory: it holds on to memory it's not using any more for a while so it can be easily re-used for new objects --- this is more efficient than giving the memory back to the operating system only to request it again shortly afterwards. > > -----Original Message----- > From: Peter Otten [mailto:__peter__@web.de] > Sent: Tuesday, August 02, 2011 4:26 PM > To: python-list@python.org > Subject: Re: python reading file memory cost > > Chris Rebert wrote: > >>> The running result was that read a 500M file consume almost 2GB RAM, >>> I cannot figure it out, somebody help! >> >> If you could store the floats themselves, rather than their string >> representations, that would be more space-efficient. You could then >> also use the `array` module, which is more space-efficient than lists >> (http://docs.python.org/library/array.html ). Numpy would also be >> worth investigating since multidimensional arrays are involved. >> >> The next obvious question would then be: do you /really/ need /all/ of >> the data in memory at once? > > This is what you (OP) should think about really hard before resorting to the > optimizations mentioned above. Perhaps you can explain what you are doing > with the data once you've loaded it into memory? > >> Also, just so you're aware: >> http://docs.python.org/library/sys.html#sys.getsizeof > > To give you an idea how memory usage explodes: > >>>> line = "1.23 4.56 7.89 0.12\n" >>>> len(line) # size in the file > 20 >>>> sys.getsizeof(line) > 60 >>>> formatted = ["%2.6E" % float(x) for x in line.split()] >>>> sys.getsizeof(formatted) + sum(sys.getsizeof(s) for s in formatted) > 312 > > > >