Memory required for struct array

8 visualizaciones (últimos 30 días)
Bruno Luong
Bruno Luong el 22 de Ag. de 2023
Comentada: Walter Roberson el 22 de Ag. de 2023
Walter Roberson gives some number about memory used by struct
it seems to me the numbers derived by Walter are not correct.
In this thread I attempt to fix, mostly by reverse engineering. Note that this are not documented and possibly depend . on the MATLAB version. Currently it's R2023a.
So there is no question explicitly formulated here

Respuestas (1)

Bruno Luong
Bruno Luong el 22 de Ag. de 2023
Editada: Bruno Luong el 22 de Ag. de 2023
The function structbase_bytesize_fun that computes the "base" memory of the struct s is as following:
fieldname_bytesize = namelengthmax() + 1; % == 64
pointer_bytesize = 8;
mxArray_byteSize = 96;
structbase_bytesize_fun = @(s) length(fieldnames(s)) * ...
(fieldname_bytesize + pointer_bytesize*numel(s));
When the struct array is allocated without specify meaningful field values, the field values are affect to [] and no more memory is allocated.
s=struct('f1',cell(1,10),'f2',cell(1,10))
s = 1×10 struct array with fields:
f1 f2
whos s
Name Size Bytes Class Attributes s 1x10 288 struct
structbase_bytesize_fun(s)
ans = 288
When a field of a structure is set by a rhs the byte size of the rhs must be added
rhs = (1:3);
rhsinfo = whos('rhs');
s(end).f1 = rhs;
whos s
Name Size Bytes Class Attributes s 1x10 408 struct
struct_bytesize = structbase_bytesize_fun(s) + rhsinfo.bytes + mxArray_byteSize
struct_bytesize = 408
rhs2 = 1:4;
rhs2info = whos('rhs2');
[s(1:5).f2] = deal(rhs2);
whos s
Name Size Bytes Class Attributes s 1x10 1048 struct
struct_bytesize = structbase_bytesize_fun(s) + ...
1*(rhsinfo.bytes + mxArray_byteSize) + ... % s(end).f1
5*(rhs2info.bytes + mxArray_byteSize) % s(1:5).f2]
struct_bytesize = 1048
NOTE that on top of that memory seen by whos command, one need to count the memory taken header of s itself, which is hide by whos, meaning 96 bytes (mxArray_byteSize) needs to be added on top of that struct_bytesize.
It is not clear to me if shared data is correctly count (reduced memory), for example
[s(1:5).f2] = deal(rhs2);
would s(1:5).f2 share one copy of the rhs somewhere?
  4 comentarios
Bruno Luong
Bruno Luong el 22 de Ag. de 2023
OK actually the number of extra bytes is
8 * (numberoffields * numel(s))
The fields data pointers are 2D array.
I fix my code.
Walter Roberson
Walter Roberson el 22 de Ag. de 2023
At the moment, I do not immediately see a difference between your size predictions and my previous enumeration?
My discussion was slightly sloppy in taking about sizes for additional entries, under the assumption that at least one entry was assigned to for each fieldname; it probably would have been better to rewrite them to a base of no entries written to. On the other hand, when no entries at all are allocated to a field, it looks like the 8-bytes-per-element pointer table is not allocated at all.

Iniciar sesión para comentar.

Categorías

Más información sobre Structures en Help Center y File Exchange.

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by