Programming and Data Types    

Managing Memory

This section covers the following topics:

For more information: See Making Efficient Use of Memory in the MATLAB "Programming and Data Types" documentation

Useful Functions for Managing Memory

Several functions that are useful in managing memory are listed below.

Function
Description
clear
Remove variables from memory.
pack
Save existing variables to disk, and then reload them contiguously.
save
Selectively store variables to disk.
load
Reload a data file saved with the save function.
quit
Exit MATLAB and return all allocated memory to the system.

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.

For more information: See Ways to Conserve Memory in the MATLAB "Programming and Data Types" documentation

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 no are longer using from memory. Use clear to do this.

Conserving Memory 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.

Matrix Manipulation with Sparse Matrices

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.

For more information: See Converting Full Matrices into Sparse in the MATLAB "Programming and Data Types" documentation, and the sparse function reference page

Structure of Arrays Rather Than 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.

Preallocating Is Better Than Growing an Array

for and while loops that incrementally increase, or grow, the size of a data structure each time through the loop can adversely affect memory usage and performance. On each iteration, MATLAB has to allocate more memory for the growing matrix and also move data in memory whenever it cannot allocate a contiguous block. This can also result in fragmentation or in used memory not being returned to the operating system.

To avoid this, preallocate a block of memory large enough to hold the matrix at its final size. For example,

For more information: See Preallocating Arrays in the MATLAB "Programming and Data Types" documentation

Use repmat When You Need to Grow Arrays

If you do need to grow an array, see if you can do it using the repmat function. repmat gets you a contiguous block of memory for your expanding array.

Preallocating a Nondouble Matrix

When you preallocate a block of memory to hold a matrix of some type other than double, it is more memory efficient and sometimes faster to use the repmat function for this.

The statement below uses zeros to preallocate a 100-by-100 matrix of uint8. It does this by first creating a full matrix of doubles, and then converting the matrix to uint8. This costs time and uses memory unnecessarily.

Using repmat, you create only one double, thus reducing your memory needs.

For more information: See Preallocating Arrays in the MATLAB "Programming and Data Types" documentation

System-Specific Ways to Use Less Memory

If you run out of memory often, you can allocate your larger matrices earlier in the MATLAB session and also use these system-specific tips:

For UNIX systems, we recommend that your machine be configured with twice as much swap space as you have RAM. The UNIX command, pstat -s, lets you know how much swap space you have.

For more information: See Platform-Specific Memory Topics in the MATLAB "Programming and Data Types" documentation

Out of Memory Errors on UNIX

On UNIX systems, you may get Out of Memory errors when executing an operating system command from within MATLAB (using the shell escape (!) operator). This is because, when a process shells out to a subprocess, UNIX allocates as much memory for the subprocess as has been allocated for the parent process.

For more information: See Running External Programs in the MATLAB "Development Environment" documentation

Reclaiming Memory on UNIX

On UNIX systems, MATLAB does not return memory to the operating system even after variables have been cleared. This is due to the manner in which UNIX manages memory. UNIX does not accept memory back from a program until the program has terminated. So, the amount of memory used in a MATLAB session is not returned to the operating system until you exit MATLAB.

To free up the memory used in your MATLAB session, save your workspace variables, exit MATLAB, and then load your variables back in.

Out of Memory Errors and the JVM

You are more likely to encounter Out of Memory errors when using MATLAB 6.0 or greater, due to the use of the Java virtual machine within MATLAB. On UNIX systems, you can reduce the amount of memory MATLAB uses by starting MATLAB with the -nojvm option. When you use this option, you cannot use the desktop or any of the MATLAB tools that require Java.

Type the following at the UNIX prompt:

For more information: See Running MATLAB without the Java Virtual Machine in the MATLAB "Programming and Data Types" documentation

Memory Requirements for Cell Arrays

The memory requirement for an M-by-N cell array containing the same type of data is

So a 5-by-6 cell array that contains a 10-by-10 real matrix in each cell takes up 27,000 bytes.

Memory Required for Cell Arrays and Structures

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.

Preallocating Cell Arrays to Save Memory

Preallocation of cell arrays is recommended if you know the data type of your cells' components. This makes it unnecessary for MATLAB to grow the cell array each time you assign values.

For example, to preallocate a 1000-by-200 cell array of empty arrays, use

For more information: See Preallocating Arrays in the MATLAB "Programming and Data Types" documentation


  Input/Output Optimizing for Speed