Maximum array size in MATLAB?

17,057

Solution 1

Take a look at this page, it lists the maximum sizes: Max sizes

It looks to be on the order of a few hundred million. Note that the matrix you're trying to create here is: 10e6 * 10e6 = 10e12 elements. This is many orders of magnitude greater than the max sizes provided and you also do not have that much RAM on your system.

My suggestion is to look into a different algorithm for what you are trying to accomplish.

Solution 2

To find out the real maximum array size (Windows only), use the command user = memory. user.maxPossibleArrayBytes shows how many bytes of contiguous RAM are free. Divide that by the number of bytes per element of your array (8 for doubles) and you know the max number of elements you can preallocate.

Note that as woodchips said, Matlab may have to copy your array (if you pass by value to a subfunction, for example). In my experience 75% of the max possible array is usually available multiple times.

Solution 3

The Limits

There are two different limits to be aware of:

  1. Maximum array size (in terms of number of elements) allowed by MATLAB, regardless of current memory availability.
  2. Current bytes available for a single array -- the (current) maximum possible array size in bytes.

The first limit is what causes "Maximum variable size allowed by the program is exceeded", not the second limit. However the second one is also a practical limit of which you must be aware!

Checking the Limits

The maximum number of elements allowed for an array is checked as follows:

>> [~,maxsize] = computer
maxsize =
   2.8147e+14

According to the documentation for the computer command, this returns:

maximum number of elements allowed in a matrix on this version of MATLAB

This is a static MATLAB limit on number of elements, not affected by the state of the computer (hardware specs and current memory usage). And at over 2 petabytes for a double array of that length, it's also way higher than any computer of which I am aware!

On the other hand, the largest practical array size that you can create at any given moment can be checked by the memory command:

>> memory
Maximum possible array:     35237 MB (3.695e+10 bytes) *
Memory available for all arrays:     35237 MB (3.695e+10 bytes) *
Memory used by MATLAB:      9545 MB (1.001e+10 bytes)
Physical Memory (RAM):     24574 MB (2.577e+10 bytes)

*  Limited by System Memory (physical + swap file) available.

As the message says, these values are based on actual current memory availability, taking into account both physical memory and the swap file (collectively, virtual memory).

If needed, these values can accessed programmatically by m = memory;.

Adjusting the Limits

The first limit (the hard limit) has been fixed up until R2015a, where it can now be changed (but only reduced to a fraction of system memory) through the following setting:

enter image description here

You can't increase it beyond your system limits.

The second limit obviously has no "setting" in MATLAB since it's based on available memory and computer configuration. Aside from adding RAM, there's not a lot you can do: (1) pack to consolidate workspace memory and perform "garbage collection", but this may only help on certain platforms, and (2) increasing page file size to allow other stuff to swap out and give MATLAB more physical memory. But be cautious when relying on your page file as your computer may become unresponsive if page file thrashing happens.

Share:
17,057
Pieter
Author by

Pieter

Updated on June 19, 2022

Comments

  • Pieter
    Pieter almost 2 years

    I'm writing a MATLAB program that will generate a matrix with 1 million rows and an unknown amount of columns (at max 1 million).

    I tried pre-allocating this matrix:

    a=zeros(1000000,1000000)
    

    but I received the error:

    "Maximum variable size allowed by the program is exceeded."

    I have a feeling that not pre-allocating this matrix will seriously slow the code down.

    This made me curious: What is the maximum array size in MATLAB?

    Update: I'm going to look into sparse matrices, because the result I am aiming for in this particular problem will be a matrix consisting for the larger part of zeros.

  • Amro
    Amro about 14 years
    I agree with the above, just wanted to point out that MATLAB uses a copy-on-write mechanism when passing matrices to functions, meaning that unless that function changes the input its as if it were passed by reference.
  • chappjc
    chappjc about 9 years
    This command still works, but is not the same as the maximum possible array size, which should be checked with m = memory; m.MaxPossibleArrayBytes. Still, the error "Maximum variable size allowed by the program is exceeded" is not even related to this limit. It's related to a hard limit on the number of elements in an array, not a memory availability issue but an implementation limit. I've explained both concepts in detail here.
  • Royi
    Royi about 7 years
    Could one set the Memory Percentage Limit by syntax instead of the UI?