The following functions can help you to manage memory use while running the MATLAB® software:
or returns information about how much memory is available and how
much is used by MATLAB. This includes the following:
Size of the largest single array MATLAB can create at this time.
Total size of the virtual address space available for data.
Total amount of memory used by the MATLAB process for both libraries and data.
Available and total Virtual Memory for the MATLAB software process.
Available system memory, including both physical memory and paging file.
Available and total physical memory (RAM) of the computer.
whos shows how much memory MATLAB has
allocated for variables in the workspace.
pack saves existing
variables to disk, and then reloads them contiguously. This reduces
the chances of running into problems due to memory fragmentation.
clear removes variables
from memory. One way to increase the amount of available memory is
to periodically clear variables from memory that you no longer need.
stores variables to the disk. This is a useful technique when you
are working with large amounts of data. Save data to the disk periodically,
and then use the
clear function to remove the saved
data from memory.
load reloads a data
file saved with the
quit exits MATLAB and
returns all allocated memory to the system. This
can be useful on UNIX® systems, which do not free up memory allocated
to an application (for example, MATLAB) until the application
Saving any needed variables with the
Quitting MATLAB to free all memory allocated to MATLAB.
Starting a new MATLAB session and loading the saved variables back into the clean MATLAB workspace.
The whos command can give you an idea of the memory used by MATLAB variables.
A = ones(10,10); whos Name Size Bytes Class Attributes A 10x10 800 double
Note that whos does not include information about
Memory used by MATLAB (for example, Java® code and plots).
Memory used for most objects (e.g., time series, custom).
Memory for variables not in the calling workspace.
Shared data copies.
bytes used for a shared data copy even when it does not use any memory.
This example shows that
whos reports an array
A) and a shared data copy of that array (
separately, even though the data exists only once in memory:
Store 400 MB array as
A. Memory used = 381MB
(715 MB – 334 MB):
memory Memory used by MATLAB: 334 MB (3.502e+008 bytes) A = rand(5e7,1); memory Memory used by MATLAB: 715 MB (7.502e+008 bytes) whos Name Size Bytes Class Attributes A 50000000x1 400000000 double
B and point it to
whos shows both
there is only one copy of the data in memory. No additional memory
is consumed by assigning
B = A; memory Memory used by MATLAB: 715 MB (7.502e+008 bytes) whos Name Size Bytes Class Attributes A 50000000x1 400000000 double B 50000000x1 400000000 double
B(1)copies all of
changes the value of
B(1). Memory used = 382MB
(1097 MB – 715 MB). Now there are two copies of the data in
memory, yet the output of whos does not change:
B(1) = 3; memory Memory used by MATLAB: 1097 MB (1.150e+009 bytes) whos Name Size Bytes Class Attributes A 50000000x1 400000000 double B 50000000x1 400000000 double