Proper Syntax and Usage for ArcGIS “in_memory” Workspace – Detailed Guide

arcgis-desktoparcpyin-memorypython

I am unclear how best to use the in_memory workspace in ArcGIS. The best resource I have found on the subject is from ESRI on Using in-memory workspace. Unfortunately, the help section is not very informative. Also, there is a post on this site that briefly touches on the subject here and here.

My specific questions:

  • What is the proper usage and syntax for using in_memory workspace in
    ArcGIS/arcpy based scripts?
  • Is in_memory workspace the same as, for example, creating a layer
    using arcpy.MakeFeatureLayer_management()?
  • Are there any standards such as deleting in_memory workspace at the end of
    the script?

Best Answer

I've been using "in_memory" quite a bit recently. It can be very useful, as it has the potential to dramatically increase processing speeds for certain tasks, however if you are working with very large datasets, it might cause your program to crash.

You can use "in_memory" to define process outputs... often, if I am performing a task on a feature class, I will copy it to the "in_memory" workspace first:

inFeature = r'C:\myDir.gdb\myFeature'
memoryFeature = "in_memory" + "\\" + "myMemoryFeature"
arcpy.CopyFeatures_management(inFeature, memoryFeature)

Note that you don't have to concatenate memoryFeature together like I did, you could write it out as "in_memory\myMemoryFeature", I just like doing it that way to switch back and forth between "in_memory" and a physical directory easily. You can then perform processes on your feature in memory. When you are done, you can reverse the process to save it back to a directory.

I could be wrong, but I believe it is not the same as creating a feature layer. feature layers give you access to selection methods and other layer specific operations. Think of the "in_memory" directory as the vector equivalent to the raster object (raster = arcpy.Raster(myRasterLocation))

To clean up after using "in_memory" simply add the following line of code:

arcpy.Delete_management("in_memory")

Hope that helps.

Related Question