How to avoid ArcMap crash due to GDI Objects > 10000 in python script?

Discussion created by wcs.org_kfisher on Apr 15, 2013
Latest reply on Apr 17, 2013 by wcs.org_kfisher

I've got an arcpy script that essentially iterates over a polygon feature class and for each polygon extracts a raster, users RasterToNumPyArray to create a NumPy array, then does a few things with the values and writes to a postgres db. Everything works fine for small numbers of features.

Despite using del to destroy variables at the end of each loop (following ArcGIS help at http://resources.arcgis.com/en/help/main/10.1/index.html#//002z00000028000000), this script generates large numbers of GDI objects (which I view in Task Manager), probably because of RasterToNumPyArray, and generally exceeds the default Windows maximum of 10000 after 2-300 polygons. This causes ArcMap to crash entirely.

To deal with this, I changed the GDI object quota on my machine, but this is obviously not a longterm solution:

I'm using ArcGIS 10.1 for Desktop SP1; there was a related bug fixed in a 10.0 service pack:

My question is: what is the preferred strategy for avoiding GDI Object overruns (ie memory leaks), other than using del to explicitly destroy variables (which doesn't seem to work), with particular reference to RasterToNumPyArray? Has anybody else run into this?