ArcGIS applications, ArcGIS Pro and ArcMap, as well as ArcGIS Server have a memory-based workspace where output feature classes, tables, and raster datasets can be written: the in_memory workspace. The in_memory workspace can be used as an alternative to writing output to files, or file and enterprise geodatabases. Writing to the in_memory workspace is often significantly faster, but not always. Data written to the in_memory workspace is temporary and will be lost when the application is closed, making in_memory ideal for intermediate data created from model and Python script tools.
To write to the in-memory workspace, specify an output dataset path beginning with in_memory, for example, in_memory/outputParcels.
Using the in_memory workspace with ArcGIS Pro
ArcGIS Pro is a 64-bit, multithreaded application. Any 64-bit software program can leverage more RAM than a 32-bit software application, which limits each application to 3 GB of RAM. Running single geoprocessing tools one at a time from either the Geoprocessing pane or the Python window does not make good use of the in_memory workspace; in fact, doing so might incur a performance cost. Because ArcGIS Pro is a multithreaded application, it can leverage multiple CPUs to perform more than one task at a time. A geoprocessing operation that writes to the in_memory workspace holds the result of that operation in a particular piece of memory that is not available to other threads. The output must be persisted to disk (in a folder or geodatabase) so all threads can act upon the output. If you write the output from a single geoprocessing operation to in_memory, the output is created there and copied to a geodatabase before being displayed in the application. As previously stated, the in_memory workspace should only be used for creating intermediate data within models and scripts. A model or script tool will execute in a single thread and have access to the same in_memory workspace; a series of chained tools will generally perform faster than a series of tools reading and writing from disk.
Managing the in_memory workspace
When using the in_memory workspace, you may want to delete any intermediate data as soon as possible to free up system memory. The Delete tool can be used to delete data in the in_memory workspace. Individual datasets can be deleted, or the entire workspace can be deleted to clear all the workspace contents. The empty in_memory workspace will continue to be available for further use.
Using in_memory with models
When creating models, tools are connected through input and output parameters. Data that is the output of one tool and used as the input for further processing in another tool is generally referred to as intermediate data. Writing intermediate outputs to the in_memory workspace may reduce the overall execution time of your model when used in ArcGIS Pro. An added benefit of constructing a model that uses the in_memory workspace is that when shared as a web tool to Portal for ArcGIS, it will continue to use the in_memory workspace when executed on the server.
Outputs defined as parameters are added to the map by default when a model is executed as a tool. These outputs can be written to the in_memory workspace; however, they will be converted to a feature class in a local geodatabase when the tool executes. Instead of creating output in the in_memory workspace and ultimately having it converted into a feature class, you may want to write model parameters to your %scratchGDB% or another full path on disk so the conversion happens only once.
Using in_memory with Python and arcpy
Using the in_memory workspace with Python script tools gives you the same benefits as using model tools. Writing intermediate data to the in_memory workspace will generally be faster compared to writing output to disk. The following example creates intermediate datasets in the in_memory workspace for the Buffer and Erase tools. The FinalOutput from the Dissolve tool creates a feature class in the Habitat geodatabase as defined in the workspace setting.
Creating output in the in_memory workspace
import arcpy # Set the geoprocessing workspace arcpy.env.workspace = r"C:\Data\Habitat.gdb" # Buffer a Roads layer, writing output to in_memory arcpy.Buffer_analysis("Roads", "in_memory/Buffers", 1000) # Erase the buffers from a Vegetation layer arcpy.Erase_analysis("Vegetation", "in_memory/Buffers", "in_memory/Erased") # Dissolve the in_memory output of Erase to make a final output arcpy.Dissolve_management("in_memory/Erased", "FinalOutput")