<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Memory leak in loops in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385728#M30425</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Will let you know how it goes. I have looked at TaskManager and the memory is not building up.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 01 Feb 2012 01:59:32 GMT</pubDate>
    <dc:creator>LyniseWearne</dc:creator>
    <dc:date>2012-02-01T01:59:32Z</dc:date>
    <item>
      <title>Memory leak in loops</title>
      <link>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385718#M30415</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi all,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have recently written a python script which involves some heavy computation. It first performs an intersection with an underlying base layer and writes the result to the memory workspace and then calculates the area for each resultant part after the intersection and summarizes them. I know the python garbage collector doesn???t work, so I explicitly deleted all intermediate variables after each loop. However, what I have experienced is still an ever increasing consumption of memory after each loop, which, given a large dataset, eventually leads to ???out of memory??? error and crash.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Any comment is appreciated!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is part of the code (the LOOP!):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
for eachspecies in specieslist:

&amp;nbsp;&amp;nbsp;&amp;nbsp; counter += 1&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # for each species, make a layer and use that layer to intersect&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; layer = str(counter)
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; whereclause = "\"" + speciesfield + "\" = '" + iucnlib.SingeQuoteToDoubleSQL(str(eachspecies)) + "\'" 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.makefeaturelayer(shp, layer, whereclause)
&amp;nbsp;&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; iucnlib.Printboth("Error: Making layer for species %s failed! Skipping this species..."%str(eachspecies))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; iucnlib.Printboth(gp.getmessage(2))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del layer
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; continue
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; intersectshp = 'in_memory' + '\\tmp' + str(counter)

&amp;nbsp;&amp;nbsp;&amp;nbsp; # spatial intersection&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.intersect_analysis(layer + ';' + ecobaselayer, intersectshp)
&amp;nbsp;&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; iucnlib.Printboth("Error: Intersection for species %s failed! Skipping this species..."%str(eachspecies))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; iucnlib.Printboth(gp.getmessage(2))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del intersectshp
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; continue

&amp;nbsp;&amp;nbsp;&amp;nbsp; #calculating areas for each geometry, using coordinates system specified by users&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; dict = iucnlib.CalcuateTotalArea(intersectshp, "REALM", spatialref)

&amp;nbsp;&amp;nbsp;&amp;nbsp; totalarea = 0&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; for eachkey in dict.keys():
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; totalarea += dict[eachkey]/1000000
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; #output result text string
&amp;nbsp;&amp;nbsp;&amp;nbsp; msg = str(eachspecies)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for each in list:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if dict.has_key(each):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # in square kilometres
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; msg = msg + ',' + '%.2f'%(dict[each]/1000000) + ',' + '%.2f'%(100*(dict[each]/1000000)/totalarea) +'%'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # default no value '0'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; msg = msg + ',' + str(0) + ',' + str(0)
&amp;nbsp;&amp;nbsp;&amp;nbsp; msg = msg + '\n'
&amp;nbsp;&amp;nbsp;&amp;nbsp; iucnlib.Log_output(msg, result)
&amp;nbsp;&amp;nbsp;&amp;nbsp; iucnlib.Printboth("Species %s has finished"%str(eachspecies))

&amp;nbsp;&amp;nbsp;&amp;nbsp; # delete intermediate variables
&amp;nbsp;&amp;nbsp;&amp;nbsp; del intersectshp, layer&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Dec 2010 08:31:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385718#M30415</guid>
      <dc:creator>YICHUANSHI</dc:creator>
      <dc:date>2010-12-14T08:31:01Z</dc:date>
    </item>
    <item>
      <title>Re: Memory leak in loops</title>
      <link>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385719#M30416</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Why make them in memory? Try writing to disk and see if that helps. It may be slower but it also may complete.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Dec 2010 12:55:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385719#M30416</guid>
      <dc:creator>ChrisMathers</dc:creator>
      <dc:date>2010-12-14T12:55:01Z</dc:date>
    </item>
    <item>
      <title>Re: Memory leak in loops</title>
      <link>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385720#M30417</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Or at least delete the in_memory output (after writting it to disk).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In my experience, there is only so much space in the RAM (about 1.7 GB) before the in_memory workspace fills up.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Dec 2010 13:22:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385720#M30417</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2010-12-14T13:22:23Z</dc:date>
    </item>
    <item>
      <title>Re: Memory leak in loops</title>
      <link>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385721#M30418</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks very much for your advice.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To clm42: There is a huge performance improvement when i start dumping intermediate results to the memory workspace. I haven't run the whole dataset writing intermediate results to disk and i'll give it a try. The thing that bothers me is it seems after each loop the memory consumption increases by 1Mb. I don't know if that has anything to do with writing to memory or disk. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To&amp;nbsp; Chris: I did remove the reference variable to the in-memory data after each loop. I think it should do it. Otherwise I would expect the memory filled up much much earlier.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Dec 2010 14:52:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385721#M30418</guid>
      <dc:creator>YICHUANSHI</dc:creator>
      <dc:date>2010-12-14T14:52:54Z</dc:date>
    </item>
    <item>
      <title>Re: Memory leak in loops</title>
      <link>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385722#M30419</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;As you may have surmised, the Python garbage collector knows nothing about ArcGIS's in-memory workspace and will not delete data from it just because you deleted the variable referencing it. You need to actually delete the data using the geoprocessor (e.g. gp.Delete_management).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;According to the help, you can either delete individual tables or feature classes in the in-memory workspace, or delete the entire in-memory workspace itself.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002w0000005s000000.htm"&gt;http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002w0000005s000000.htm&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Dec 2010 17:41:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385722#M30419</guid>
      <dc:creator>LoganPugh</dc:creator>
      <dc:date>2010-12-14T17:41:21Z</dc:date>
    </item>
    <item>
      <title>Re: Memory leak in loops</title>
      <link>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385723#M30420</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I did remove the reference variable to the in-memory data after each loop. I think it should do it.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Like Logan said (and what I meant in my 1st message), you have to explicitly delete the in_memory feature class ( gp.Delete_Managment("in_memory\\output_4") ). It takes up RAM, just like an on-disk FC would take up disk space. Deleting the variable:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;del myFC&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Does not do anything at all to free up the RAM...&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Dec 2010 17:46:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385723#M30420</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2010-12-14T17:46:43Z</dc:date>
    </item>
    <item>
      <title>Re: Memory leak in loops</title>
      <link>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385724#M30421</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks very much lpugh01 and Chris! As you said, it didn't delete the in-memory fc when i drop the reference. Problem solved, very much appreciated it. I think ESRI should put this caveat in relation to Python somewhere more explicitly. It took me a whole day trying to figure out and test (I didn't even think that this could be wrong)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Dec 2010 07:49:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385724#M30421</guid>
      <dc:creator>YICHUANSHI</dc:creator>
      <dc:date>2010-12-15T07:49:20Z</dc:date>
    </item>
    <item>
      <title>Re: Memory leak in loops</title>
      <link>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385725#M30422</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Glad that did the trick!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;FYI - From the "in_memory workspace" help topic &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002w0000005s000000.htm:"&gt;http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002w0000005s000000.htm:&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;When data is written to the in-memory workspace, the computer's physical memory (RAM) is consumed. If too much data is written to this workspace, all the computer's memory may be used up and additional data cannot be written to memory.&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Dec 2010 14:55:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385725#M30422</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2010-12-15T14:55:05Z</dc:date>
    </item>
    <item>
      <title>Re: Memory leak in loops</title>
      <link>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385726#M30423</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi all, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am having a similar problem as described below and think this is related to memory leaks. The script runs succssfullly for about 8 hours then shuts down in the middle of processing. Can someone please have a look at the attached script, any suggestions would be appreciated.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Lynise&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Jan 2012 22:47:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385726#M30423</guid>
      <dc:creator>LyniseWearne</dc:creator>
      <dc:date>2012-01-31T22:47:46Z</dc:date>
    </item>
    <item>
      <title>Re: Memory leak in loops</title>
      <link>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385727#M30424</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Note sure what the issue is... Used to be in pre v10.0, you could run SpatialAnlayst tools in a loop for a year and they never had any memory issues.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Maybe don't delete the scratch files every loop, just let arcpy overwrite them and delete them at the very end (unindent all your arcpy.Delete statements 1 tab), and include this line of code before your loop:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;arcpy.env.overwriteOutput = True&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Take note of the memory usuage in the task manger... Does it just build and build with each loop? &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;How about calling a seperate process for each loop... See:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://forums.arcgis.com/threads/33602-Arcpy-Multiprocessor-issues"&gt;http://forums.arcgis.com/threads/33602-Arcpy-Multiprocessor-issues&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You can look for other Python-based parallel processing examples in the forums using the keywords "os.spawnv", "subprocess", "multiprocessing", or "parallel python".&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Jan 2012 23:37:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385727#M30424</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2012-01-31T23:37:07Z</dc:date>
    </item>
    <item>
      <title>Re: Memory leak in loops</title>
      <link>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385728#M30425</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Will let you know how it goes. I have looked at TaskManager and the memory is not building up.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Feb 2012 01:59:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/memory-leak-in-loops/m-p/385728#M30425</guid>
      <dc:creator>LyniseWearne</dc:creator>
      <dc:date>2012-02-01T01:59:32Z</dc:date>
    </item>
  </channel>
</rss>

