This topic explains several techniques to use memory efficiently in MATLAB®.
MATLAB provides you with different sizes of data classes, such as
uint8, so you do not need to
use large classes to store your smaller segments of data. For example, it takes 7 KB
less memory to store 1,000 small unsigned integer values using the
uint8 class than it does with
The numeric class you should use in MATLAB depends on your intended actions. The default class
double gives the best precision, but requires 8 bytes per
element of memory to store. If you intend to perform complicated math such as
linear algebra, you must use a floating-point class such as a
single class requires only 4 bytes. There are some
limitations on what you can do with the
single class, but
most MATLAB Math operations are supported.
If you just need to carry out simple arithmetic and you represent the original data as integers, you can use the integer classes in MATLAB. The following is a list of numeric classes, memory requirements (in bytes), and the supported operations.
|Class (Data Type)||Bytes||Supported Operations|
|1||Arithmetic and some simple functions|
|2||Arithmetic and some simple functions|
|4||Arithmetic and some simple functions|
|8||Arithmetic and some simple functions|
MATLAB arrays (implemented internally as
require room to store meta information about the data in memory, such as type,
dimensions, and attributes. This takes about 104 bytes per array. This overhead
only becomes an issue when you have a large number (e.g., hundreds or thousands)
mxArrays (e.g., scalars). The
whos command lists the memory
used by variables, but does not include this overhead.
Because simple numeric arrays (comprising one
the least overhead, you should use them wherever possible. When data is too
complex to store in a simple array (or matrix), you can use other data
Cell arrays are comprised of separate
mxArrays for each
element. As a result, cell arrays with many small elements have a large
Structures require a similar amount of overhead per field (see Array Headers). Structures with many fields and small contents have a large overhead and should be avoided. A large array of structures with numeric scalar fields requires much more memory than a structure with fields containing large numeric arrays.
Also note that while MATLAB stores numeric arrays in contiguous memory, this is not the case for structures and cell arrays.
When reading data from a binary file with
fread, it is a common error to
specify only the class of the data in the file, and not the class of the data
MATLAB uses once it is in the workspace. As a result, the default
double is used even if you are reading only 8-bit values.
fid = fopen('large_file_of_uint8s.bin', 'r'); a = fread(fid, 1e3, 'uint8'); % Requires 8k whos a Name Size Bytes Class Attributes a 1000x1 8000 double a = fread(fid, 1e3, 'uint8=>uint8'); % Requires 1k whos a Name Size Bytes Class Attributes a 1000x1 1000 uint8
If your data contains many zeros, consider using sparse arrays, which store only nonzero elements. The following example compares sparse and full storage requirements:
A = eye(1000); % Full matrix with ones on the diagonal As = sparse(A); % Sparse matrix with only nonzero elements whos Name Size Bytes Class Attributes A 1000x1000 8000000 double As 1000x1000 24008 double sparse
You can see that this array requires only about 4 KB to be stored as sparse,
but approximately 8 MB as a full matrix. In general, for a sparse double array
nnz nonzero elements and
columns, the memory required is:
nnz + 8 *
ncol + 8 bytes
(on a 64-bit machine)
Note that MATLAB supports most, but not all, mathematical operations on sparse arrays.
You can significantly reduce the amount of memory required by avoiding the creation of unnecessary temporary copies of data.
Avoid creating large temporary variables, and also make it a practice to clear
temporary variables when they are no longer needed. For example, this code
creates an array of zeros stored as a temporary variable
and then converts
A to single-precision:
A = zeros(1e6,1); As = single(A);
It is more memory efficient to use one command to do both operations:
A = zeros(1e6,1,'single');
When working with large data sets, be aware that MATLAB makes a temporary copy of an input variable if the called function modifies its value. This temporarily doubles the memory required to store the array, which causes MATLAB to generate an error if sufficient memory is not available.
One way to use less memory in this situation is to use nested functions. A
nested function shares the workspace of all outer functions, giving the nested
function access to data outside of its usual scope. In the example shown here,
setrowval has direct access to the workspace
of the outer function
myfun, making it unnecessary to pass a
copy of the variable in the function call. When
modifies the value of
A, it modifies it in the workspace of
the calling function. There is no need to use additional memory to hold a
separate array for the function being called, and there also is no need to
return the modified value of
function myfun A = magic(500); function setrowval(row, value) A(row,:) = value; end setrowval(400, 0); disp('The new value of A(399:401,1:10) is') A(399:401,1:10) end
One simple way to increase the amount of memory you have available is to clear large arrays that you no longer use.
If your program generates very large amounts of data, consider writing the
data to disk periodically. After saving that portion of the data, use the
clear function to remove the
variable from memory and continue with the data generation.
When you are working with a very large data set repeatedly or interactively, clear the old variable first to make space for the new variable. Otherwise, MATLAB requires temporary storage of equal size before overriding the variable. For example,
a = rand(1e5); b = rand(1e5); Out of memory. More information clear a a = rand(1e5); % New array