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?
