Question regarding how matrices are stored in memory
8 views (last 30 days)
Marcus Rosales on 1 Sep 2021
I'm trying to get a simulation running which requires a big matrix ( N~400), so being smart with memory is important.
The issue: Sparing the details of what I am doing I generate this matrix, call it S, and then multiply it by a number Δ. When I do this, I get an error that matlab doesn't support array sizes this big.
Now if I decide to measure my energy scale in respect to Δ, I do not need to multiply S by it, and this matrix fits in memory. However, the number of elements in my matrix is the same... This got me thinking: how are numbers stored in matrices here?
My thoughts: I always thought that memory is allocated when you define a matrix: we have a floating point number with 64 bits of memory for each element in our matrix.
This situation makes it seem like an matrix with all zeros takes less memory than one with all 2.718281828459045's. Of course the latter requires more to describe than the former; I just thought things were preallocated.
My questions: Does matlab only keep track of the decimal and significant figures and the fewer of these the smaller? Or does it cast variables to a smaller size or something automatically? Or is it the case that what I said in the previous paragraph is right regarding allocating a fixed amount of memory at the start? Or something else...
Walter Roberson on 1 Sep 2021
I have demonstrated through experiments that at least on MacOS and Linux, if you allocate an array using zeros(), that it does not ask the operating system for all of that memory at that time. However as soon as you store even one thing into the array, it then needs to ask the operating system for all of the memory, and as a result you can get an "out of memory" error. For example,
A = zeros(2.71e9,1);
A(1) = 1;
With this online facility, 2.72e9 would give out of memory at the zeros; I have shown in the past that sometimes it would be the assignment into A(1) that would trigger the out of memory.
I did not test on MS Windows.
However, this is specifically an "out of memory" error that is generated, not an "array is too large" error.
The MATLAB model is that any array that is created and is not sparse, has all of its memory allocated at the time of creation. But that turns out not to be the case when zeros() specifically is used, as long as the array stays just 0.
If is generating an array too big error, that most often implies that Δ is a vector or array and that the inner-product operator * needs size(S,1) * size(Delta,2) memory locations for the result. So if you accidentally * a column vector and a row vector, you can be asking for a much larger result than you expect.
If is generating an array too big error, that most often implies that implicit expansion is being done. If you accidentally .* a column vector and a row vector, you could be asking for a much larger result than you expect.
So... recheck whether you are using the * (inner product) or .* (element-by-element) operator, and recheck size() of the arrays to see whether implicit expansion might be happening.