I have a script written in Python that uses ArcPy to create maps of the kernel densities of thousands of datasets. Each dataset is relatively small, but there about 50,000 sets in total.

When I run my script, it stops after about 10-15 minutes and gives the "python.exe has stopped working" popup. The Windows 7 event viewer tells me it is an Exception Code 0x0000005, which appears to be an access violation. The datasets the code works on are not the same each time, so it is not a specific set that is causing the problem.

Watching the task manager when the script is running, I noticed that python.exe is increasing its memory usage every few seconds, which makes me believe there is a memory leak somewhere. I thought Python had garbage collection? Is there something that arcPy is doing?

EDIT: So I tracked down the part of my code causing the problem, but I don't know HOW it is leaking. I think I am cleaning everything up. Does anyone spot something I missed?

def makeMap(asno, isKdens):
    mxd = arcpy.mapping.MapDocument("F:\\testFiles\\testMap.mxd")   ###Reference to basemap document
    df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]     ###Get data frame reference in var
    if isKdens:
        ###Set up layer file
        lyrFile = arcpy.mapping.Layer(r"F:\\testFiles\\output\\AS_"+asno+"_KDens.lyr")
        ###Set up layer file
        lyrFile = arcpy.mapping.Layer(r"F:\\testFiles\\output\\AS_"+asno+".lyr")
        lyrFile.visible = True
    ###Add layer file
    arcpy.mapping.AddLayer(df, lyrFile)

    ###ref added layer
    lyr = arcpy.mapping.ListLayers(mxd)[0]
    lyr.visible = True

    #If this is a raw map, just select points.
    if isKdens==0:
        arcpy.SelectLayerByAttribute_management ("pointslyr", "NEW_SELECTION", "\"asno\" = "+asno+"")
    #If KDens map, apply symbology
        arcpy.ApplySymbologyFromLayer_management(lyr, templateLayer)

    ###Zoom to selection
    lyrExtent = lyr.getSelectedExtent()
    df.extent = lyrExtent

    ###Export to PDF!!!
    if isKdens:
        arcpy.mapping.ExportToPDF(mxd, r"F:\\testFiles\\output\\AS_"+asno+"_KDens_Map.pdf")
        arcpy.mapping.ExportToPDF(mxd, r"F:\\testFiles\\output\\AS_"+asno+"_Map.pdf")
    print arcpy.GetMessages()
del mxd, df, lyrFile, lyrExtent, lyr

EDIT 2 So, I also tried turning off logging, since I have heard that can cause memory leaks, too. There is still something leaking. I also tried using Delete_management instead of del, but that just messes things up. It almost seems like my code is not running sequentially. A delete after a reference still gives a "references before defined" error...

Anyway, now I am trying to locate the memory leak using memory analyzer packages. Out of all of them that work with Python, I only got objgraph to work. I placed a objgraph.show_growth call before and after the function whose code is above. Here are the results:


tuple                         11959    +11959
function                       3643     +3643
dict                           1632     +1632
wrapper_descriptor             1086     +1086
cell                            927      +927
list                            635      +635
builtin_function_or_method      590      +590
property                        551      +551
method_descriptor               390      +390
weakref                         373      +373


weakref                 398       +25
wrapper_descriptor     1105       +19
tuple                 11977       +18
dict                   1644       +12
member_descriptor       172        +4
getset_descriptor       213        +3
method_descriptor       392        +2
instance                 15        +2
list                    636        +1
`_RLock                    2        +1

It doesn't look like any of my objects are leaking, though I am not quite sure what instance means in the second growth dump. Anyone have any insights?

  • 5
    Maybe you could post some pseudocode?
    – Aaron
    Feb 11, 2013 at 23:12
  • 3
    Are you creating lots of MapDocument objects and not cleaning them up as you go by using del?
    – PolyGeo
    Feb 11, 2013 at 23:49
  • 1
    Also, where is the output of the kernel density going?
    – om_henners
    Feb 11, 2013 at 23:59
  • 2
    I have no experience of ArcPy, but some of the geoprocessing tools in earlier versions of ArcGIS do leak memory. Python does have garbage collection and, in my experience, memory leaks in ArcGIS scripts are rarely due to Python itself; they're either due to programmer error or to leaky geoprocessing tools. Some options for profiling memory consumption in your Python code are described here
    – JamesS
    Feb 12, 2013 at 10:13
  • @PolyGeo I have updated my original post to answer your question (as well as om_henners') and to provide a bit more information about my code.
    – N-C
    Feb 13, 2013 at 18:56

1 Answer 1


Just use the Delete tool. Call arcpy.management.Delete("rdlayer") after saving the layer file.

  • Thanks. I added this after saving the layer file, but python.exe still crashes. There must be some other leak elsewhere, but at least I took care of one of them!
    – N-C
    Feb 13, 2013 at 20:27
  • export a pdf 2 times with the same mxd without arcpy block my arcmap. It maybe the same problem. I solution it by closing mxd and reopen Nov 26, 2015 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.