# Memory usage and management

### Overview

The evaluation of some models may require large amounts of memory, especially when they contain arrays with high dimensionality. When the memory needed by your model approaches the available memory on your computer, you may encounter an error informing you that there is insufficient memory to carry out the computation, or you may experience a general slowdown as the process starts utilizing virtual memory. To address the challenges presented by memory bottlenecks, you may need to rethink various aspects of how you are attacking your problem, or you may have to employ various advanced modeling techniques to obtain the desired computations.

### Memory limits

When you evaluate a model that requires more memory than your computer can provide, your experience is likely to be different depending on whether you are running the 32-bit or 64-bit edition of Analytica. Analytica 32-bit, like any 32-bit process[1], is limited to a maximum memory usage of 2GB, 3GB or 4GB, depending on the operating system. When this limit is reached, Analytica will report an insufficient memory error. With the 64-bit edition, which does not have this same limit[2], you are more likely to encounter a slowdown when the model’s memory requirements substantially exceeds the amount of RAM on your computer. The model continues to evaluate, but the computer is slowed as it swaps pages of memory between a page file on your hard drive and RAM.

### Configuring available memory

Various settings in the Windows operating system determine the maximum amount of memory that your process can use before running out of memory.

In 32-bit editions of Windows, processes are limited to 2GB of space unless you explicitly configure a flag in the C:\boot.ini file. With the /3GB flag in boot.ini, processes like Analytica may use up to 3GB (see How To Access More Memory on the Analytica Wiki). Analytica 32-bit may be used from a 64-bit edition of Windows, in which case it can utilize 4GB with no special configuration required.

If you need to utilize more memory than the amount of RAM on your computer, you may need to adjust your virtual memory settings. On Windows, configure this from My Computer → Properties → Advanced system settings → Performance → Settings → Advanced → Virtual Memory (Change). You can substantially improve performance by installing a solid state hard drive (SSDI) and locating your page file on that drive. You should configure a custom size with an initial size that is large enough to accommodate your large computations. We find that Windows freezes for several minutes when it needs to increase the size of its page file, which you avoid by using a suitably large initial size.

The above virtual memory settings illustrates our recommended settings. On this computer, the paging file has been located on the solid state drive E: for maximum speed. To ensure it uses the SSDI, drive C: is set to No paging file. A Custom size for E: has been specified -- this part is important. We strongly recommend against using the System managed size option (unless you enjoy having Windows freeze for several minutes). Set the initial size to something large enough to accommodate the largest memory usage you might ever encounter. In this example, the maximum size has been set to something a bit larger, but in reality there is little benefit to setting the Maximum size to anything larger than the Initial size, since you should never exceed the initial size.

### Viewing memory usage

You can monitor the total amount of memory in use by your Analytica process by using the Memory Usage dialog. See also Memory usage.

When you find your model consuming excessive amounts of memory, the first step is to determine where it is using that memory, and track it to a particular variable. For that, the Performance Profiler library is especially useful. This is a feature available in Analytica Enterprise, and with it, you can determine how much memory is being consumed by each variable in your model. It is not uncommon to find that a small handful of variables consume the bulk of memory, and knowing that, you can focus your efforts on those variables.

### Reduce dimensionality

Excessive memory usage is most often the result of excessive dimensionality. Analytica makes it very easy to add indexes to your model, but the effect is multiplicative in space and time when those indexes appear in the same array. Finding the right trade-off between dimensionality, level of detail and resource usage (time and computation) is an inherent aspect of model building.

When reducing dimensionality, focus on individual arrays that consume a lot of space as a result of high dimensionality. Can you reformulate your problem using a lower-dimensional array?

### Selective parametric analysis

Parametric analysis refers to the practice of inserting an extra index into a model input in order to explore how outputs vary across a range of values for the given input. Analytica makes it easy to do this simultaneously for multiple inputs, but in the process, the dimensionality of computed results increases. Selective parametric analysis is the technique of performing parametric analysis on only a few inputs at a time. All other inputs are left at a single point; hence, only a few parametric indexes are combined in any one evaluation. The user of the model then repeats the process, selecting a new subset of inputs for parametric analysis, and re-evaluating the model. You can facilitate selective parametric analysis by utilizing Choice menu input controls for potential parametric inputs. See Creating a choice menu.

### Configuring caching

Whenever Analytica computes the result of a variable, it stores the result in memory. If the value is requested later by another variable, or if the user views the result, Analytica simply returns the previously computed result without having to recompute it again. This is referred to as caching.

It may be unnecessary to cache many of the intermediate variables within your model, or to keep the cached value once all the children of that variable have been computed. You can configure how cached results are retained by setting the CachingMethod attribute for each variable[3]. You can configure a variable to always cache its results, never cache, never cache array values, or release cached results after all children are fully computed. To control the CachingMethod, you must first make the attribute visible as described in Managing attributes. Note that you should always cache results for any variable containing a random or distribution function. There are some other limitations and interactions to be aware of when managing caching policies, as described further in Controlling When Result Values Are Cached on the Analytica Wiki.

### Looping over the model

Analytica’s array abstraction computes the entire model in one pass. Because all intermediate variables are normally cached, this means that the results for all scenarios, across all indexes, are stored in memory for all variables.

An alternative is to loop over key dimensions, computing the entire model for a single case at a time, and collecting only the results for final output variable in each case. This consumes only the amount of memory required to compute the entire model for a single scenario, plus the memory for the final result.

The basic technique is illustrated by the following example. Suppose your model has an input X indexed by I, and an output Y. We also assume that no intermediate steps in the model operate over the index I. Then Y by looping using:

For xi[ ] := X do WhatIf(Y, X, xi)

This simple example is illustrative. As you apply this to complex models involving multiple inputs, outputs and looping dimensions, a greater level of sophistication becomes necessary. The article Looping over a model on the Analytica Wiki covers this topic in greater detail.

### Large sample library

Monte Carlo simulations with a very large sample size may also lead to insufficient memory. The Large sample library runs a Monte Carlo simulation in small batches, collecting the entire Monte Carlo sample for a selected subset of output variables. The Large Sample Library User Guide is found on the Analytica Wiki, where the library itself is also available for download.

### CompressMemoryUsedBy(A)

In some cases, the CompressMemoryUsedBy() function is able to reduce the amount of memory consumed by array A. The logical contents of A remains unchanged, so you will not see a difference, other than possibly a drop in memory usage.

Analytica’s internal representation of arrays is able to accommodate certain forms of sparseness, and when such patterns of sparseness occur within A, CompressMemoryUsedBy(A) condenses the internal representation to leverage the sparseness. There are two forms of sparseness that may occur: Shared subarrays and constant subvectors. For more details on these forms of sparseness, see CompressMemoryUsedBy on the Analytica Wiki.

## Notes

1. A 32-bit process runs in a process space where each byte is identified by its 32-bit address. With 32-bits, it is possible to have 232 distinct locations, limiting the process to 4 billion bytes
2. The 264 ceiling for a 64-bit process, which is about 18 million terabytes, will not be the limiting factor. Your virtual memory settings on Windows determine the maximum memory available. Various editions of Windows also impose varying limits on how much memory can be accessed. For example, Windows 7 Professional limits processes to 192GB.
3. Use of CachingMethod requires Analytica Enterprise.