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:
clear
removes variables from memory.pack
saves existing variables to disk, then reloads them contiguously. Because of time considerations, you should not use pack
within loops or M-file functions.quit
exits MATLAB and returns all allocated memory to the system.save
selectively stores variables to disk.load
reloads a data file saved with the save
command.Note
save and load are faster than MATLAB low-level file I/O routines. save
and load have been optimized to run faster and reduce memory
fragmentation.
|
On some systems, the whos
function displays the amount of free memory remaining. However, be aware that:
whos
usually does not get larger unless the deleted variable occupied the highest memory addresses. The number actually indicates the amount of contiguous, unused memory. Clearing the highest variable makes the number larger, but clearing a variable beneath the highest variable has no effect. This means that you might have more free memory than is indicated by whos
.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:
clear
command.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.
result = function2(function1(input99)); result = function1(input99); result = function2(result);
Variables and Memory
Memory is allocated for variables whenever the left-hand side variable in an assignment does not exist. The statement
x = 10
allocates memory, but the statement
x(10) = 1
does not allocate memory if the 10th element of x
exists.
[]
to free memory, or clear them using
clear variable_name
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.
global a a = 5;
Now there is one copy of a
stored in the MATLAB workspace. Typing
clear a
removes a
from the MATLAB workspace, but it still exists in the global
workspace.
clear global a
removes a
from the global workspace.
PC-Specific Topics
UNIX-Specific Topics
For example, on one machine these statements use approximately 15.4 MB of RAM.
a = rand(1e6,1); b = rand(1e6,1);
These statements use approximately 16.4 MB of RAM.
c = rand(2.1e6,1);
These statements use approximately 32.4 MB of RAM.
a = rand(1e6,1); b = rand(1e6,1); clear c = rand(2.1e6,1);
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 preallocate the largest vector. This series of statements uses approximately 32.4 MB of RAM
a = rand(1e6,1); b = rand(1e6,1); clear c = rand(2.1e6,1);
while these statements use only about 16.4 MB of RAM
c = rand(2.1e6,1); clear a = rand(1e6,1); b = rand(1e6,1);
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) | ![]() |