ans =
327.6349e+006
Make a copy of array A in B. As there is no need to have two copies of the array data,
MATLAB only makes a copy of the array reference. This requires no significant additional
memory:
B = A;
memUsed
ans =
327.6349e+006
Now modify B by making it one half its original size (that is, set 1000 rows to empty). This
requires that MATLAB make a copy of at least the first 1000 rows of the A array, and
assign that copy to B:
B(1001:2000,:) = [];
format short; size(B)
ans =
1000 2000
Check the memory used again. Even though B is significantly smaller than it was
originally, the amount of memory used by the MATLAB process has increased by about 16
MB (1/2 of the 32 MB originally required for A) because B could no longer remain as just
a reference to A:
format short eng; memUsed
ans =
343.6421e+006
Array Headers
When you assign an array to a variable, MATLAB also stores information about the array
(such as class and dimensions) in a separate piece of memory called a header. For most
arrays, the memory required to store the header is insignificant. There is a small
advantage to storing large data sets in a small number of large arrays as opposed to a
large number of small arrays. This is because the former configuration requires fewer
array headers.
Structure and Cell Arrays
For structures and cell arrays, MATLAB creates a header not only for each array, but also
for each field of the structure and for each cell of a cell array. Because of this, the amount
29 Memory Usage