Programming and Data Types |

This section discusses various ways that may help you to use less memory and avoid `Out`

`of`

`Memory`

errors in MATLAB.

**Working with Variables**

To conserve memory when creating variables,

- Avoid creating large temporary variables, and clear temporary variables when they are no longer needed.
- When working with arrays of fixed size, preallocate them rather than having MATLAB resize the array each time you enlarge it.
- Set variables equal to the empty matrix
`[]`

to free memory, or clear the variables using the`clear`

function. - Reuse variables as much as possible.

**Compressing Data in Memory**

Since MATLAB uses a heap method of memory management, extended MATLAB sessions may cause memory to become fragmented. When memory is fragmented, there may be plenty of free space, but not enough contiguous memory to store a new large variable. If you get the `Out`

`of`

`Memory`

message from MATLAB, the `pack`

function may be able to compress some of your data in memory, thus freeing up larger contiguous blocks.

**Clearing Unused Variables from Memory**

If you use `pack`

and there is still not enough free memory to proceed, you probably need to remove some of the variables you are no longer using from memory. Use `clear`

to do this.

**Working with Large Amounts of Data**

If your program generates very large amounts of data, consider writing the data to disk periodically. After saving that portion of the data, `clear`

the variable from memory and continue with the data generation.

**Converting Full Matrices into Sparse**

Matrices with values that are mostly zero are best stored in `sparse`

format. Sparse matrices can use less memory and may also be faster to manipulate than full matrices. You can convert a full matrix to sparse using the `sparse`

function.

Compare two 1000-by-1000 matrices: `X`

, a matrix of doubles with 2/3 of its elements equal to zero; and `Y`

, a sparse copy of `X`

. As shown below, approximately half as much memory is required for the sparse matrix.

whos Name Size Bytes Class X 1000x1000 8000000 double array Y 1000x1000 4004000 double array (sparse)

**Memory Requirements for Cell Arrays**

Contiguous memory is not required for an entire cell array or structure array. Since each of these is actually an array of pointers to other arrays, the memory for each array needs to be contiguous, but the entire memory collection does not need to be.

**Comparing a Structure of Arrays with an Array of Structures**

If your MATLAB application needs to store a large amount of data, and the definition of that data lends itself to being stored in either a structure of arrays or an array of structures, the former is preferable. A structure of arrays requires significantly less memory than an array of structures, and also has a corresponding speed benefit.

**Nested Function Calls**

The amount of memory used by nested functions is the same as the amount used by calling them on consecutive lines. These two examples require the same amount of memory.

Making Efficient Use of Memory | "Out of Memory" Errors |