Sycosys

My program likes to slowly cause Arcmap to run out of ram and crash

Discussion created by Sycosys on Oct 18, 2012
Latest reply on Oct 22, 2012 by mzcoyle
I run a whole mess of queries one after another and create some data. On each iteration the ram usage climbs by a few megabytes. This is all well and good if I only want to run ~120 queries, if I run more I risk crashing out.

anything I can do to stop the memleak?


Code in case it's useful.
import arcpy
from arcpy import env
arcpy.env.overwriteOutput = True

#Set Workspace
workSpace = "C:/Users/atimpson/Desktop/PNG HCA Program Dev/Workspace.mdb"
env.workspace = workSpace
layerName = "StatisticsLine"

#Query Column Options
clQuery =  ['[dot_classification] = "CLASS_1"','[dot_classification] = "CLASS_2"','[dot_classification] = "CLASS_3"','[dot_classification] = "CLASS_4"']
hcaQuery = ['[UniqueID] IS NOT NULL','[UniqueID] IS NULL']

#Query Row Options
aQuery =['[Query_A] = "Report"','([Query_A] = "Unknown" or [Query_A] IS NULL)']
bQuery =['[Query_B] > 0', '([Query_B] = 0 or [Query_B] is Null)']
cQuery =['[Query_C] = "Report"','([Query_C] = "Unknown" or [Query_C] IS Null)']
dQuery =['[Query_D] > 0', '([Query_D] = 0 or [Query_D] is Null)']
eQuery =['[Query_E] > 0', '([Query_E] = 0 or [Query_E] is Null)']
fQuery =['[Query_F] = "Report"','([Query_F] = "Unknown" or [Query_F] IS Null)']
gQuery =['[Query_G] = "Report"','([Query_G] = "Unknown" or [Query_G] IS Null)']

class1HCA =   []
class1NoHCA = []
class2HCA =   []
class2NoHCA = []
class3HCA =   []
class3NoHCA = []
class4HCA =   []
class4NoHCA = []

def tableWriter():
 if cl == '[dot_classification] = "CLASS_1"':
      
  if hca == '[UniqueID] IS NOT NULL':
   tables = arcpy.SearchCursor("Temp_Stats", "", "", "SUM_Shape_Length", "")
   table = tables.next()
   print table.getValue("SUM_Shape_Length")
   value = str(table.getValue("SUM_Shape_Length"))
   class1HCA.append(value)
       
  if hca == '[UniqueID] IS NULL':
   tables = arcpy.SearchCursor("Temp_Stats", "", "", "SUM_Shape_Length", "")
   table = tables.next()
   print table.getValue("SUM_Shape_Length")
   value = str(table.getValue("SUM_Shape_Length"))
   class1NoHCA.append(value)
     
 if cl == '[dot_classification] = "CLASS_2"':
      
  if hca == '[UniqueID] IS NOT NULL':
   tables = arcpy.SearchCursor("Temp_Stats", "", "", "SUM_Shape_Length", "")
   table = tables.next()
   print table.getValue("SUM_Shape_Length")
   value = str(table.getValue("SUM_Shape_Length"))
   class2HCA.append(value)
  if hca == '[UniqueID] IS NULL':
   tables = arcpy.SearchCursor("Temp_Stats", "", "", "SUM_Shape_Length", "")
   table = tables.next()
   print table.getValue("SUM_Shape_Length")
   value = str(table.getValue("SUM_Shape_Length"))
   class2NoHCA.append(value)
     
 if cl == '[dot_classification] = "CLASS_3"':
      
  if hca == '[UniqueID] IS NOT NULL':
   tables = arcpy.SearchCursor("Temp_Stats", "", "", "SUM_Shape_Length", "")
   table = tables.next()
   print table.getValue("SUM_Shape_Length")
   value = str(table.getValue("SUM_Shape_Length"))
   class3HCA.append(value)
       
  if hca == '[UniqueID] IS NULL':
   tables = arcpy.SearchCursor("Temp_Stats", "", "", "SUM_Shape_Length", "")
   table = tables.next()
   print table.getValue("SUM_Shape_Length")
   value = str(table.getValue("SUM_Shape_Length"))
   class3NoHCA.append(value)
       
 if cl == '[dot_classification] = "CLASS_4"':
      
  if hca == '[UniqueID] IS NOT NULL':
   tables = arcpy.SearchCursor("Temp_Stats", "", "", "SUM_Shape_Length", "")
   table = tables.next()
   print table.getValue("SUM_Shape_Length")  
   value = str(table.getValue("SUM_Shape_Length"))
   class4HCA.append(value)
       
  if hca == '[UniqueID] IS NULL':
   tables = arcpy.SearchCursor("Temp_Stats", "", "", "SUM_Shape_Length", "")
   table = tables.next()
   print table.getValue("SUM_Shape_Length")
   value = str(table.getValue("SUM_Shape_Length"))
   class4NoHCA.append(value)
 del tables, table
 
 
def resultHandler():
 if count == 0:
  print "No Query Results- Appending a placeholder"
  rows = arcpy.InsertCursor("Temp_Stats")
  x = 0
  while x != 1:
   row = rows.newRow()
   row.Frequency = 0
   rows.insertRow(row)
   x = 1
  query = searchStr.replace("\"","")
  arcpy.CalculateField_management("Temp_Stats", "Query", "'"+ query +"'"  , "PYTHON")
  arcpy.Append_management(["Temp_Stats"], "Stats", "TEST", "", "")
  tableWriter()  
  del row, rows
  arcpy.Delete_management(workSpace + "/Temp_Stats")
    
 else:
  query = searchStr.replace("\"","")
  arcpy.CalculateField_management("Temp_Stats", "Query", "'"+ query +"'"  , "PYTHON")
  arcpy.Append_management(["Temp_Stats"], "Stats", "TEST", "", "")
  tableWriter()       
  arcpy.Delete_management(workSpace + "/Temp_Stats") 
   
try:
 arcpy.SelectLayerByAttribute_management (layerName, "NEW_SELECTION", "[ObjectID]>=0")
 arcpy.Statistics_analysis(layerName, "Stats", [["Shape_Length", "SUM"]])
 arcpy.AddField_management("Stats", "Query", "TEXT", "", "", 254)
 arcpy.CalculateField_management("Stats", "Query",'"[ObjectID] >= 0"', "PYTHON")
 
 
 for cl in clQuery:
  for hca in hcaQuery:
   for a in aQuery:   
    arcpy.SelectLayerByAttribute_management (layerName, "NEW_SELECTION", cl + " and " + hca + " and " +  a)
    searchStr = str(cl + " and " + hca + " and " +  a)
    print searchStr
    arcpy.Statistics_analysis(layerName, "Temp_Stats", [["Shape_Length", "SUM"]])
    count = int(arcpy.GetCount_management("Temp_Stats").getOutput(0))
    arcpy.AddField_management("Temp_Stats", "Query", "TEXT", "", "", 254)
    resultHandler()

 for cl in clQuery:
  for hca in hcaQuery:
   for b in bQuery:   
    arcpy.SelectLayerByAttribute_management (layerName, "NEW_SELECTION", cl + " and " + hca + " and " +  b)
    searchStr = str(cl + " and " + hca + " and " +  b)
    print searchStr
    arcpy.Statistics_analysis(layerName, "Temp_Stats", [["Shape_Length", "SUM"]])
    count = int(arcpy.GetCount_management("Temp_Stats").getOutput(0))
    arcpy.AddField_management("Temp_Stats", "Query", "TEXT", "", "", 254)
    resultHandler()
 
 for cl in clQuery:
  for hca in hcaQuery:
   for c in cQuery:   
    arcpy.SelectLayerByAttribute_management (layerName, "NEW_SELECTION", cl + " and " + hca + " and " +  c)
    searchStr = str(cl + " and " + hca + " and " +  c)
    print searchStr
    arcpy.Statistics_analysis(layerName, "Temp_Stats", [["Shape_Length", "SUM"]])
    count = int(arcpy.GetCount_management("Temp_Stats").getOutput(0))
    arcpy.AddField_management("Temp_Stats", "Query", "TEXT", "", "", 254)
    resultHandler()
 
 for cl in clQuery:
  for hca in hcaQuery:
   for d in dQuery:   
    arcpy.SelectLayerByAttribute_management (layerName, "NEW_SELECTION", cl + " and " + hca + " and " +  d)
    searchStr = str(cl + " and " + hca + " and " +  d)
    print searchStr
    arcpy.Statistics_analysis(layerName, "Temp_Stats", [["Shape_Length", "SUM"]])
    count = int(arcpy.GetCount_management("Temp_Stats").getOutput(0))
    arcpy.AddField_management("Temp_Stats", "Query", "TEXT", "", "", 254)
    resultHandler()
 
 for cl in clQuery:
  for hca in hcaQuery:
   for e in eQuery:   
    arcpy.SelectLayerByAttribute_management (layerName, "NEW_SELECTION", cl + " and " + hca + " and " +  e)
    searchStr = str(cl + " and " + hca + " and " +  e)
    print searchStr
    arcpy.Statistics_analysis(layerName, "Temp_Stats", [["Shape_Length", "SUM"]])
    count = int(arcpy.GetCount_management("Temp_Stats").getOutput(0))
    arcpy.AddField_management("Temp_Stats", "Query", "TEXT", "", "", 254)
    resultHandler()

 for cl in clQuery:
  for hca in hcaQuery:
   for f in fQuery:   
    arcpy.SelectLayerByAttribute_management (layerName, "NEW_SELECTION", cl + " and " + hca + " and " +  f)
    searchStr = str(cl + " and " + hca + " and " +  f)
    print searchStr
    arcpy.Statistics_analysis(layerName, "Temp_Stats", [["Shape_Length", "SUM"]])
    count = int(arcpy.GetCount_management("Temp_Stats").getOutput(0))
    arcpy.AddField_management("Temp_Stats", "Query", "TEXT", "", "", 254)
    resultHandler()

 for cl in clQuery:
  for hca in hcaQuery:
   for g in gQuery:   
    arcpy.SelectLayerByAttribute_management (layerName, "NEW_SELECTION", cl + " and " + hca + " and " +  g)
    searchStr = str(cl + " and " + hca + " and " +  g)
    print searchStr
    arcpy.Statistics_analysis(layerName, "Temp_Stats", [["Shape_Length", "SUM"]])
    count = int(arcpy.GetCount_management("Temp_Stats").getOutput(0))
    arcpy.AddField_management("Temp_Stats", "Query", "TEXT", "", "", 254)
    resultHandler()
    
 print class1HCA
 print class1NoHCA
 print class2HCA
 print class2NoHCA
 print class3HCA
 print class3NoHCA
 print class4HCA
 print class4NoHCA
 
 f = open("C:/Users/atimpson\Desktop/PNG HCA Program Dev/Output/Test.txt","w")
 f.write(str(class1HCA) +"\n")
 f.write(str(class1NoHCA) +"\n")
 f.write(str(class2HCA) +"\n")
 f.write(str(class2NoHCA) +"\n")
 f.write(str(class3HCA) +"\n")
 f.write(str(class3NoHCA) +"\n")
 f.write(str(class4HCA) +"\n")
 f.write(str(class4NoHCA) +"\n")
 f.close()
 
except Exception as e:
 print e.message
 print("Something bad has happened")

#del aQuery, bQuery, cQuery, dQuery, eQuery,fQuery, gQuery, class1HCA, class1NoHCA, class2HCA, class2NoHCA, class3HCA, class3NoHCA, class4HCA, class4NoHCA, hcaQuery, clQuery     

Outcomes