Programming Previous page   Next Page

Strategies for Efficient Use of Memory

To conserve memory when creating variables,

Preallocating Arrays to Reduce Fragmentation

In the course of a MATLAB session, memory can become fragmented due to dynamic memory allocation and deallocation. for and while loops that incrementally increase, or grow, the size of a data structure each time through the loop can add to this fragmentation as they have to repeatedly find and allocate larger blocks of memory to store the data.

To make more efficient use of your memory, preallocate a block of memory large enough to hold the matrix at its final size before entering the loop. When you preallocate memory for a potentially large array, MATLAB "grabs" sufficient contiguous space for the data at the beginning of the computation. Once you have this space, you can add elements to the array without having to continually allocate new space for it in memory.

For more information on preallocation, see Preallocating Arrays.

Allocating Large Matrices Earlier

MATLAB uses a heap method of memory management. It requests memory from the operating system when there is not enough memory available in the MATLAB heap to store the current variables. It reuses memory as long as the size of the memory segment required is available in the MATLAB heap.

For example, on one machine these statements use approximately 15.4 MB of RAM:

This statement uses approximately 16.4 MB of RAM:

These statements use approximately 32.4 MB of RAM. This is because MATLAB is not able to fit a 2.1 MB array in the space previously occupied by two 1-MB arrays:

The simplest way to prevent overallocation of memory is to allocate the largest vectors first. These statements use only about 16.4 MB of RAM:

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, use the clear function to remove the variable from memory and continue with the data generation.


Previous page  Data Structures and Memory Resolving "Out of Memory" Errors Next page

© 1994-2005 The MathWorks, Inc.