MATLAB Answers

Memory allocation for structure array

1 view (last 30 days)
Anna
Anna on 7 May 2020
Edited: James Tursa on 13 May 2020
I am trying to understand how memory allocation works for different kinds of variables.
For structure arrays I have found the formula:
64-bit systems: fields x ((112 x array elements) + 64) + data
but I don't understand where 112 and 64 come from.

  0 Comments

Sign in to comment.

Answers (1)

James Tursa
James Tursa on 7 May 2020
Edited: James Tursa on 7 May 2020
The 112 I think is an estimate of the size of an mxArray header (the internal structure for each variable that MATLAB uses behind the scenes to store type, size, data pointers, etc.). Each variable in a struct could have a unique one. In the header hacks I use in my mex routines I get a different number, either 96 or 88 for 64-bit MATLAB depending on the version. But this number is uncertain outside of TMW since they stopped publishing this in their distributed mex headers several years ago. The actual storage could be less if some of the variables are shared reference copies of other variables (i.e., sharing the mxArray header).
The 64 I think is a max value for the string used to store a field name (63 for the string itself and 1 for the null character at the end of a C-style string). The field names could be less than that, of course, and they can also be sharing names with other variables.
The data memory can also be shared with other struct variables. Also realize that data will not in reality be allocated on single byte boundaries behind the scenes. And sparse matrix storage can be allocated to be more than the actual data present. Etc. So there is some fuzziness in those data numbers.
Bottom line is the formula should be viewed as approximate, and as a max storage footprint if nothing in the struct is shared with other variables. If I were to guess at such a formula, I might use this instead for 64-bit MATLAB:
(number of fields) * ( 8 * (number of struct array elements) + 64) +
(1 + number of non-NULL elements in the struct) * (96 or 88) +
(total amount of data storage for the elements)
The 8 is the storage needed for the pointers to the struct array elements. 96 would be used for R2017b and earlier, and 88 would be used for R2018a and later (TMW got rid of the seperate Pi complex data pointer when they went to interleaved complex).

  4 Comments

Show 1 older comment
James Tursa
James Tursa on 11 May 2020
Which version of MATLAB? What is the exact structure of the variable? What is the source of your formula?
Stephen Cobeldick
Stephen Cobeldick on 13 May 2020
Anna's incorrectly posted "Answer" moved here:
The Matlab version is R2018a, Update 6.
I have taken a very simple example:
A.a=[1 1]
A.b=1
that consists of 2 fields, 1 array element and therefore the formula gives:
2(112*1+64)+24=376
I have found the formula at:
James Tursa
James Tursa on 13 May 2020
I think that 112 number may have changed over the years, but certainly it seems correct for later versions of MATLAB.
However, the formula as presented in the doc is a bit misleading. EACH distinct variable in the struct above has its own header, and the struct itself has a header. So your A above definitely has three distinct headers (A, A.a, A.b), so the formula should have 3*112 in it but I don't see that.
Also, the formula in the doc glosses over the fact that some of those spots can be NULL and don't take any memory. And it doesn't count the data memory of A itself (the pointers for the field elements). I would calculate the actual minimum memory footprint at:
2*(8*1 + 64) + 3*112 + 3*8 = 504
But even that 64 is misleading based on my understanding of how field names are stored, which seems to be in a packed array in separate memory.
So, bottom line is the number reported by whos seems to match the doc for this case, but it still should be regarded as approximate since it doesn't account for everything.

Sign in to comment.


Translated by