I'm new at making scripts and I´m having some issues in my first arcpy script.

I've created a code that automates maps creation, and it's running ok but gradually increases the memory consumption until the memory become full, the process stops and arcmap exhibits an "out of memory" message.

I've used the arcpy.Delete_management() to try to clean up the memory, but is not affecting so much the memory cleaning...

Is there another way to clean the memory?

The link to the code is posted below...

I posted the link because the code is too large to paste here in gis.stackexchange


Please, remember that I´m a scripting rookie

closed as off-topic by PolyGeo Oct 26 '15 at 19:23

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Out of memory is typically associated with a logical error. That is to say that you are looping something infinitely. My reccomendation is to use a lot of print statements at the beginning of your loops to see what loops runs to infinity. put a print ('Loop 1 Running ') .... at the start of any loop, and trial and error until you find your problem, or just put it on all of them. – Martin Oct 26 '15 at 13:51
  • 3
    This is more of a general code enhancement question than a GIS specific one. In general you have to look for objects that are not needed anymore by the script and do not go out of scope - e.g. by beeing set to None, beeing deleted or automatically garbage collected after a loop is done. I suggest you read up on Python memory management and Python memory profiling to pinpoint where exactly you are creating the memory error. – Kersten Oct 26 '15 at 13:51
  • 2
    Note that arcpy.Delete_management() does not generally clean up memory -- it cleans up disk -- using del removes objects from memory in Python. If your code is too large to fit inline, then you haven't taken the time to create an MCVE. Splitting your code into standalone components to achieve the "Minimal" part will help you identify what code to evaluate. – Vince Oct 26 '15 at 14:19
  • Hello Martin... I think the problem is not about "running infinitely" because in the first "for" statement the search is made in a shapefile table... when the shapefile table has 500 lines, the script runs ok... when the shapefile table has 1000 lines, arcmap exhibits the message "out of memory"... – Gerardo F. E. Pérez Oct 26 '15 at 16:54
  • Kersten... your tip is a good one... I´ll try to use the "Python memory profiling" to look where is the memory accumulation... I tried to install it, but without success... is just download, unzip and run the command $ python setup.py install in the Python command line? – Gerardo F. E. Pérez Oct 26 '15 at 16:54

Aside from the comments about memory management techniques, I wanted to give some thoughts about the code to see if it helps.

You could look into using the arcpy.da.SearchCursor instead of the arcpy.SearchCursor as it will improve speed and possibly memory usage.

I am not entirely sure what the if impLyr01 == 1: sections are doing. You assign the variables at the top of your script, but I don't see any reassignment happening. So when you're comparing the values within the cursor like that, you are going to get the result every time. I am assuming that isn't your intent. I also see a couple calls to arcpy.RefreshActiveView() in there. You might be able to reorder your code and only call it once per iteration.

You code could use some formatting niceties done to it. For instance, when you're comparing your text_elm.text values, you could move that into a function that returns a value. It helps readability and also debugging. Another readability thing you could use is the .format() or % methods.

Aside from those pieces above to help you sort through the code, one option you may find useful is to create the majority of your variables before going into the cursor loop. This way, you're not assigning them multiple times. You may have the problem of the garbage collector not being able to keep up with the amount of items you're creating. You may want to look into using the del keyword to see if that helps clear up your memory.

Lastly, add a few messages to your script arcpy.AddMessage() so you can monitor your script's progress. You could output your memory usage, too. This would be a good way to section out your code (functions work great too) with messages to let you better get a feel for what is going on, and how much memory is being used.

Other than that, you may find the Code Review Stack Exchange a better place for this question as it is not really GIS related, but it is more python related.

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