Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #15435

RE: memory management

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <Juan.Declet-Barreto@MesaAZ.gov>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'python,': 0.01; '2.7': 0.04; 'value,': 0.04; 'cpython': 0.05; 'function,': 0.07; 'rules.': 0.07; 'space.': 0.07; 'python': 0.08; 'complicate': 0.09; 'garbage': 0.09; 'likewise,': 0.09; 'relevant.': 0.09; 'message-----': 0.12; 'things.': 0.13; 'library': 0.14; 'case.': 0.15; 'to:name:python-list@python.org': 0.15; '(when': 0.16; 'crashes': 0.16; 'earlier.': 0.16; "function's": 0.16; 'like?': 0.16; 'os.walk': 0.16; 'sizes,': 0.16; 'subject:memory': 0.16; 'wrote:': 0.18; '(perhaps': 0.18; 'example.': 0.18; '(e.g.,': 0.19; 'figure': 0.20; 'java': 0.20; 'memory': 0.20; "doesn't": 0.23; 'structure': 0.23; 'header:In-Reply-To:1': 0.23; 'variable': 0.24; "python's": 0.24; 'shell': 0.24; 'code': 0.25; 'windows': 0.26; 'function': 0.27; '(and': 0.28; 'reaches': 0.28; 'bit': 0.28; 'lists': 0.28; 'script': 0.28; 'allocated': 0.28; 'idle': 0.28; 'received:199': 0.28; 'version.': 0.28; 'environment': 0.30; 'module': 0.30; 'example': 0.30; 'cc:': 0.30; 'pm,': 0.31; 'anyone': 0.31; 'hi,': 0.31; 'adds': 0.32; 'point,': 0.32; 'does': 0.32; 'pretty': 0.32; 'list': 0.32; 'app': 0.32; 'to:addr:python- list': 0.32; 'actually': 0.33; 'it.': 0.33; 'there': 0.33; 'sent:': 0.33; 'subject:': 0.33; 'list.': 0.34; 'test': 0.34; 'probably': 0.34; 'however,': 0.34; 'running': 0.35; 'object': 0.35; 'file': 0.36; 'addition,': 0.36; 'charset:us-ascii': 0.36; 'assuming': 0.36; 'things,': 0.36; 'reference': 0.37; 'but': 0.37; 'think': 0.37; 'using': 0.37; 'back.': 0.38; 'monday,': 0.38; 'somewhat': 0.38; 'run': 0.38; 'put': 0.38; 'some': 0.38; 'difficult': 0.38; 'from:': 0.39; 'basic': 0.39; "it's": 0.39; 'tool': 0.39; 'why': 0.39; 'subject:: ': 0.39; 'might': 0.39; 'to:addr:python.org': 0.39; 'delete': 0.40; 'data': 0.40; 'really': 0.40; 'more': 0.60; 'huge': 0.60; 'your': 0.61; '2011': 0.62; 'kind': 0.62; 'thousands': 0.64; 'manager.': 0.64; 'prove': 0.64; 'ever': 0.65; 'high': 0.66; 'crash': 0.67; 'loose': 0.67; 'show': 0.67; 'received:us': 0.68; 'evaluate': 0.71; 'act.': 0.84; 'ceiling': 0.84; 'different.': 0.84; 'directories,': 0.84; 'problematic': 0.84; 'received:10.131': 0.84; 'subject:management': 0.84; 'swap': 0.84; 'timer': 0.84; 'var,': 0.84; 'xp,': 0.84; 'ships': 0.91; 'mistakes': 0.93
From Juan Declet-Barreto <Juan.Declet-Barreto@MesaAZ.gov>
To "python-list@python.org" <python-list@python.org>
Date Mon, 7 Nov 2011 13:33:09 -0700
Subject RE: memory management
Thread-Topic memory management
Thread-Index Acydiq+B6JBKcGiXTgKknLONHr0dvwAAGZkA
References <3CB1190388197146B35D522652EAB501014C71D0C4@MESAMAIL01.acctcom.mesa> <4EB83D69.2000409@dejaviewphoto.com>
In-Reply-To <4EB83D69.2000409@dejaviewphoto.com>
Accept-Language en-US
Content-Language en-US
X-MS-Has-Attach
X-MS-TNEF-Correlator
acceptlanguage en-US
Content-Type text/plain; charset="us-ascii"
Content-Transfer-Encoding quoted-printable
MIME-Version 1.0
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2513.1320697994.27778.python-list@python.org> (permalink)
Lines 65
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1320697994 news.xs4all.nl 6927 [2001:888:2000:d::a6]:35751
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:15435

Show key headers only | View raw


Well, I am using Python 2.5 (and the IDLE shell) in Windows XP, which ships with ESRI's ArcGIS. In addition, I am using some functions in the arcgisscripting Python geoprocessing module for geographic information systems (GIS) applications, which can complicate things. I am currently isolating standard library Python code (e.g., os.walk()) from the arcgisscripting module to evaluate in which module the environment crash is occurring. 

-----Original Message-----
From: Dave Angel [mailto:davea@dejaviewphoto.com] 
Sent: Monday, November 07, 2011 1:20 PM
To: Juan Declet-Barreto
Cc: python-list@python.org
Subject: Re: memory management


On 11/07/2011 02:43 PM, Juan Declet-Barreto wrote:
> Hi,
>
> Can anyone provide links or basic info on memory management, variable dereferencing, or the like? I have a script that traverses a file structure using os.walk and adds directory names to a list. It works for a small number of directories, but when I set it loose on a directory with thousands of dirs/subdirs, it crashes the DOS session and also the Python shell (when I run it from the shell).  This makes it difficult to figure out if the allocated memory or heap space for the DOS/shell session have overflown, or why it is crashing.
>
> Juan Declet-Barreto [ciId:image001.png@01CC9D4A.CB6B9D70]
I don't have any reference to point you to, but CPython's memory management is really pretty simple.  However, it's important to tell us the build of Python, as there are several, with very different memory rules.  For example Jython, which is Python running in a Java VM, lets the java garbage collector handle things, and it's entirely different.

Likewise, the OS may be relevant.  You're using Windows-kind of terminology, but that doesn't prove you're on Windows, nor does it say what version.

Assuming 32 bit CPython 2.7 on XP, the principles are simple.  When an 
object is no longer accessible, it gets garbage collected*.   So if you 
build a list inside a function, and the only reference is from a function's local var, then the whole list will be freed when the function exits.  The mistakes many people make are unnecessarily using globals, and using lists when iterables would work just as well.

The tool on XP to tell how much memory is in use is the task manager.  
As you point out, its hard to catch a short-running app in the act.  So you want to add a counter to your code (global), and see how high it gets when it crashes.  Then put a test in your code for the timer value, and do an "input" somewhat earlier.

At that point, see how much memory the program is actually using.

Now, when an object is freed, a new one of the same size is likely to immediately re-use the space.  But if they're all different sizes, it's somewhat statistical.  You might get fragmentation, for example.  When Python's pool is full, it asks the OS for more (perhaps using swap space), but I don't think it ever gives it back.  So your memory use is a kind of ceiling case.  That's why it's problematic to build a huge data structure, and then walk through it, then delete it.  The script will probably continue to show the peak memory use, indefinitely.

* (technically, this is ref counted.  When the ref reaches zero the object is freed.  Real gc is more lazy scanning)

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

RE: memory management Juan Declet-Barreto <Juan.Declet-Barreto@MesaAZ.gov> - 2011-11-07 13:33 -0700

csiph-web