Revision: 1.0
- MATLAB has five functions to improve the way in which memory is handled:
clear, pack, quit, save and load.
clear | removes variables from memory |
pack | saves existing variables to disk, then reloads them contiguously |
quit | exits MATLAB and returns all allocated memory to the system |
The following two commands can also be used to store data and retrieve data
from one session to another:
save | selectively stores variables to disk |
load | reloads a data file saved with the save command |
NOTE: Using load and save is faster than using MATLAB's file
i/o routines. save and load have been optimized to run
faster and reduce memory fragmentation.
For more information on the above commands, please refer to the MATLAB
Reference Guide.
- The amount of memory used by calling a function in this manner:
result=function2(function1(input99));
is the same as making two separate calls, such as:
result=function1(input99);
result=function2(result);
- 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
matlabpath function. MATLAB function-file code and MEX-file
relocatable code are loaded into memory when the corresponding function is
called. The function-file code or relocatable code is removed from memory when:
- the function is called again and a new version now exists
- the function is explicitly cleared with the clear command
- all functions are explicitly cleared with the clear functions command
- MATLAB runs out of memory
- Declaring variables as global merely puts a flag in a symbol table. It
does not use any more memory than defining non global variables. Consider the
following example:
a = 5;
global a
Now there is one copy of 'a' stored in the MATLAB workspace
clear a
Now 'a' has been removed from the MATLAB workspace, but it still
exists in the global workspace.
clear global a
Now 'a' has been removed from the global workspace.
- Memory is allocated for variables whenever there is an '=' in the
statement. The statement 'x = 10' allocates memory, but the statement
'x(10) = 1' does not allocate memory if the 10th element of
'x' exists.
- Avoid using the same variable as inputs and as outputs to a function. They
will be copied by reference. For example, "y = fun(x,y)" is not
preferred because y is both an input and an output variable.
- Set variables equal to the empty matrix [] to free memory, or clear them
using clear <variable>, where <variable> is the
variable name.
- Reuse variables as much as possible.
- Do not use the pack function within loops or functions.
- Close unnecessary figure windows.
- MATLAB does all of its calculations in IEEE double precision floating
point. If you do not need this much accuracy and are doing some very large
calculations, it may help to use a C language MEX-file. This allows you to do
some calculations using ints or chars. Then pass the results back into MATLAB
for display and other operations.
- There are no functions implemented to manipulate the way MATLAB handles
Windows system resources. System resources are used by the Windows operating
system to track fonts, windows, and screen objects. They can be depleted by
using multiple figure windows, multiple fonts, or several UI-controls. The
best way to free up system resources is to close all inactive windows.
Iconified windows still use resources.
- The performance of a permanent swap file is typically better than that of
a temporary swap file.
- Typically a swap file twice the size of the installed RAM is sufficient.
- Memory which MATLAB requests from the operating system is not returned to
the operating system until the MATLAB process in finished.
- MATLAB 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 in the heap as long as the size of the memory segment required is
available in the MATLAB heap.
For example, on a Sun Sparc 10 running version MATLAB 4.2:
a = rand(1e6,1);
b = rand(1e6,1);
uses approximately 15.4 Mb of RAM, and
uses approximately 16.4 Mb of RAM, while
a = rand(1e6,1);
b = rand(1e6,1);
clear
c = rand(2.1e6,1);
uses 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 the two 1 Mb arrays. The simplest way to prevent over
allocation of memory, is to pre-allocate the largest vector. Consider the
following examples:
This:
a = rand(1e6,1);
b = rand(1e6,1);
clear
c = rand(2.1e6,1);
uses approximately 32.4 Mb of RAM, and
c = rand(2.1e6,1);
clear
a = rand(1e6,1);
b = rand(1e6,1);
uses only about 16.4 Mb of RAM. Allocating the largest vectors first allows
for optimal use of the available memory.
Typically when the "Out of Memory" message appears, it is because MATLAB asked
the operating system for a segment of memory larger than that which is
currently available. Use of the above mentioned techniques: pre-allocation,
clear, pack, save, load, and quit, help optimize the
available memory. If the "Out of Memory" message still appears consistently:
- Increase the size of the swap file.
- Make sure that there are no external constraints on the memory accessible
to MATLAB (on UNIX systems use the limit command to check).
- Add more memory to the system.
- Reduce the size of the data you are using.
(c) Copyright 1996 by The MathWorks, Inc.
www.mathworks.com