Some of the Python scripts I run can take 24 - 78 hours to complete. During the beginning stages, these scripts consume a reasonable amount of Memory (about 16% - 20%). But the longer they run the more Memory they consume, until Task Manager says that 99% of the Memory is being used. (Just to clarify, this is Memory not CPU). I recently learned about garbage collection in Python, but that has not seemed to fix the problem. I have also tried
arcpy<SPAN class="punctuation token">.</SPAN>Delete_management<SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
but that has not solved the problem either.
Does anyone know how to keep the Memory from continuously building up?
Here is an example of some code where I incorporated garbage collection and arcpy.Delete_management:
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">import</SPAN> time
<SPAN class="keyword token">import</SPAN> datetime
<SPAN class="keyword token">from</SPAN> datetime <SPAN class="keyword token">import</SPAN> timedelta
<SPAN class="keyword token">import</SPAN> gc
<SPAN class="comment token">#Document Start Time in-order to calculate Run Time</SPAN>
time1 <SPAN class="operator token">=</SPAN> time<SPAN class="punctuation token">.</SPAN>clock<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#project location</SPAN>
p <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mp<SPAN class="punctuation token">.</SPAN>ArcGISProject<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">'C:\arcGIS_Shared\Python\CenterHeatMaps4.aprx'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Name of layout that the PDFs will be based on</SPAN>
lyt <SPAN class="operator token">=</SPAN> p<SPAN class="punctuation token">.</SPAN>listLayouts<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Layout_King"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token">#second object is the name of the mapframe in the project</SPAN>
mf <SPAN class="operator token">=</SPAN> lyt<SPAN class="punctuation token">.</SPAN>listElements<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"MAPFRAME_ELEMENT"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Map*"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
bkmks <SPAN class="operator token">=</SPAN> mf<SPAN class="punctuation token">.</SPAN>map<SPAN class="punctuation token">.</SPAN>listBookmarks<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
m <SPAN class="operator token">=</SPAN> p<SPAN class="punctuation token">.</SPAN>listMaps<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token">#layers to be used in the PDFs</SPAN>
OnLyrList <SPAN class="operator token">=</SPAN> m<SPAN class="punctuation token">.</SPAN>listLayers<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Topographic'</SPAN><SPAN class="punctuation token">)</SPAN>
OnNxtList <SPAN class="operator token">=</SPAN> m<SPAN class="punctuation token">.</SPAN>listLayers<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'OpenCenters'</SPAN><SPAN class="punctuation token">)</SPAN>
OnList <SPAN class="operator token">=</SPAN> m<SPAN class="punctuation token">.</SPAN>listLayers<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'SumWithin1000*'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">####Loop through all bookmarks until finding a match for the active center</SPAN>
<SPAN class="keyword token">for</SPAN> bkmk <SPAN class="keyword token">in</SPAN> bkmks<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token">#Turn off all layers</SPAN>
lyrList <SPAN class="operator token">=</SPAN> m<SPAN class="punctuation token">.</SPAN>listLayers<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> lyr <SPAN class="keyword token">in</SPAN> lyrList<SPAN class="punctuation token">:</SPAN>
lyr<SPAN class="punctuation token">.</SPAN>visible <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">False</SPAN>
<SPAN class="comment token">#Turn on desired layers</SPAN>
<SPAN class="keyword token">for</SPAN> lyr <SPAN class="keyword token">in</SPAN> OnLyrList<SPAN class="punctuation token">:</SPAN>
lyr<SPAN class="punctuation token">.</SPAN>visible <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="keyword token">for</SPAN> lyr <SPAN class="keyword token">in</SPAN> OnNxtList<SPAN class="punctuation token">:</SPAN>
lyr<SPAN class="punctuation token">.</SPAN>visible <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="keyword token">for</SPAN> lyr <SPAN class="keyword token">in</SPAN> OnList<SPAN class="punctuation token">:</SPAN>
lyr<SPAN class="punctuation token">.</SPAN>visible <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="comment token">#Zoom to active bookmark</SPAN>
mf<SPAN class="punctuation token">.</SPAN>zoomToBookmark<SPAN class="punctuation token">(</SPAN>bkmk<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">### Zoom in or out further from bookmark</SPAN>
mf<SPAN class="punctuation token">.</SPAN>camera<SPAN class="punctuation token">.</SPAN>scale <SPAN class="operator token">=</SPAN> mf<SPAN class="punctuation token">.</SPAN>camera<SPAN class="punctuation token">.</SPAN>scale <SPAN class="operator token">*</SPAN> <SPAN class="number token">1.50</SPAN>
<SPAN class="comment token">#Change Layout Title</SPAN>
<SPAN class="keyword token">for</SPAN> elm <SPAN class="keyword token">in</SPAN> lyt<SPAN class="punctuation token">.</SPAN>listElements<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"TEXT_ELEMENT"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
elm<SPAN class="punctuation token">.</SPAN>text <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Zip Code Out 50 Percent - "</SPAN> <SPAN class="operator token">+</SPAN> bkmk<SPAN class="punctuation token">.</SPAN>name <SPAN class="comment token">#[22:]</SPAN>
<SPAN class="comment token">#Export to PDF</SPAN>
lyt<SPAN class="punctuation token">.</SPAN>exportToPDF<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">"C:\arcGIS_Shared\Python\Export\Geographic_Boundaries"</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\\Zip Code Out 50 Percent "</SPAN> <SPAN class="operator token">+</SPAN> bkmk<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">+</SPAN> <SPAN class="string token">".pdf"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#+ "\\NY"</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>bkmk<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">### Clear Memory</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>Delete_management<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">"C:\arcGIS_Shared\Python\Export\CensusTracts"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">del</SPAN> lyrList<SPAN class="punctuation token">,</SPAN> OnList<SPAN class="punctuation token">,</SPAN> OnLyrList<SPAN class="punctuation token">,</SPAN> OnNxtList
gc<SPAN class="punctuation token">.</SPAN>collect<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Document End Time</SPAN>
time2 <SPAN class="operator token">=</SPAN> time<SPAN class="punctuation token">.</SPAN>clock<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Run Time in seconds</SPAN>
runtime <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>time2<SPAN class="operator token">-</SPAN>time1<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Print total run time for the job</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>timedelta<SPAN class="punctuation token">(</SPAN>seconds<SPAN class="operator token">=</SPAN>runtime<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>