MATLAB array size limit error - why?
395 views (last 30 days)
Show older comments
Andres on 18 Oct 2015
Commented: Mishary Alfarah on 13 Apr 2019
I am receiving an unexpected error because of an array allegedly exceeding the maximum array size limit with a non-double type variable.
Here is a short example to reproduce the error:
First, to keep the example variables small, go to preferences -> MATLAB -> workspace and set the limit of the maximum array size temporarily to 1% of the RAM.
You may provoke the correct error with a double array demanding too much memory:
[~,sys] = memory;
testLen = round(sys.PhysicalMemory.Total/100/2);
N = zeros(testLen,1);
Error using zeros
Requested 62859407x1 (0.5GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time and cause MATLAB to become unresponsive. See array size limit or preference panel for more information.
(for 12GB of RAM)
A uint8 array of the same length is ok:
n = zeros(testLen,1,'uint8');
returns no error. You can manipulate it by
n(end) = uint8(1);
0 0 0 0 0 1
But why do
n(end-1:end) = uint8(1);
n(end-1:end) = uint8([1;1]);
and similar manipulations error:
Requested 62859405x1 (0.5GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time and cause MATLAB to become unresponsive. See array size limit or preference panel for more information.
Is MATLAB (R2015b) simply miscalculating the array memory size for non-doubles here or am I doing something wrong?
Walter Roberson on 18 Oct 2015
n(end-1:end) = uint8(1) may require that n be duplicated if n is an array that is currently shared. For example if n is a parameter passed in, and is not being used in the restricted context
n = SomeFunction(n)
whose header is
function n = SomeFunction(n)
then MATLAB may well need to duplicate the array.
Edited: Andres on 19 Oct 2015
Mishary Alfarah on 13 Apr 2019
I uchecked that limit and on my activity monitor it showed that it took 63 GB of RAM! then it immediately quit. I think keeping it on maximum maybe is better than uncheck it.
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!