AnsweredAssumed Answered

Clear Memory Cache - ArcPy

Question asked by bisoftware on Apr 25, 2018
Latest reply on Apr 25, 2018 by Dan_Patterson

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.Delete_management

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:

import arcpy
import time
import datetime
from datetime import timedelta
import gc

#Document Start Time in-order to calculate Run Time
time1 = time.clock()

#project location
p = arcpy.mp.ArcGISProject(r'C:\arcGIS_Shared\Python\CenterHeatMaps4.aprx')
#Name of layout that the PDFs will be based on
lyt = p.listLayouts("Layout_King")[0]
#second object is the name of the mapframe in the project
mf = lyt.listElements("MAPFRAME_ELEMENT", "Map*")[0]
bkmks = mf.map.listBookmarks()
m = p.listMaps()[0]
#layers to be used in the PDFs
OnLyrList = m.listLayers('Topographic')
OnNxtList = m.listLayers('OpenCenters')
OnList = m.listLayers('SumWithin1000*')


####Loop through all bookmarks until finding a match for the active center

for bkmk in bkmks:
   
    
       
       
        #Turn off all layers
    lyrList = m.listLayers()
   
    for lyr in lyrList:
        lyr.visible = False
       

    #Turn on desired layers
    for lyr in OnLyrList:
        lyr.visible = True
    for lyr in OnNxtList:
        lyr.visible = True
    for lyr in OnList:
        lyr.visible = True


    #Zoom to active bookmark
    mf.zoomToBookmark(bkmk)
   
    ### Zoom in or out further from bookmark
    mf.camera.scale = mf.camera.scale * 1.50
   
      
    #Change Layout Title
    for elm in lyt.listElements("TEXT_ELEMENT"):

        elm.text = "Zip Code Out 50 Percent - " + bkmk.name #[22:]
       
          

    #Export to PDF
    lyt.exportToPDF(r"C:\arcGIS_Shared\Python\Export\Geographic_Boundaries" + "\\Zip Code Out 50 Percent " + bkmk.name + ".pdf") #+ "\\NY"

    print(bkmk.name)
    
    ### Clear Memory
    arcpy.Delete_management(r"C:\arcGIS_Shared\Python\Export\CensusTracts")
    del lyrList, OnList, OnLyrList, OnNxtList
    gc.collect()       
              
#Document End Time
time2 = time.clock()

#Run Time in seconds
runtime = (time2-time1)

#Print total run time for the job
print (str(timedelta(seconds=runtime)))

Outcomes