Programming and Data Types    

Making Efficient Use of Memory

This section discusses the following ways to conserve memory and improve memory use:

Memory Management Functions

MATLAB has five functions to improve how memory is handled:

On some systems, the whos function displays the amount of free memory remaining. However, be aware that:

Removing a Function From Memory

MATLAB creates a list of M- and MEX-filenames at startup for all files that reside below the matlab/toolbox directories. This list is stored in memory and is freed only when a new list is created during a call to the path function. Function M-file code and MEX-file relocatable code are loaded into memory when the corresponding function is called. The M-file code or relocatable code is removed from memory when:

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.

Variables and Memory

Memory is allocated for variables whenever the left-hand side variable in an assignment does not exist. The statement

allocates memory, but the statement

does not allocate memory if the 10th element of x exists.

To conserve memory:

Global Variables.   Declaring variables as global merely puts a flag in a symbol table. It does not use any more memory than defining nonglobal variables. Consider the following example.

Now there is one copy of a stored in the MATLAB workspace. Typing

removes a from the MATLAB workspace, but it still exists in the global workspace.

removes a from the global workspace.

PC-Specific Topics

UNIX-Specific Topics

while these statements use only about 16.4 MB of RAM

Allocating the largest vectors first allows for optimal use of the available memory.

What Does "Out of Memory" Mean?

Typically the Out of Memory message appears because MATLAB asked the operating system for a segment of memory larger than what is currently available. Use any of the techniques discussed in this section to help optimize the available memory. If the Out of Memory message still appears:


 Preallocating Arrays Character Arrays (Strings)