Memory required for struct array

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

답변 (1개)

Bruno Luong
Bruno Luong 2023년 8월 22일
편집: Bruno Luong 2023년 8월 22일

0 개 추천

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

Bruno Luong
Bruno Luong 2023년 8월 22일
Of course once you have the structure built, it's better to use whos to get the memory occupied.
The formula is useful to predict memory requirement without building the structure.
clear s
s(100).foo = [];
whos s
Name Size Bytes Class Attributes s 1x100 960 struct
clear s
s(99).foo = [];
whos s
Name Size Bytes Class Attributes s 1x99 952 struct
960 - 952
ans = 8
so the number of extra bytes for each numel() must be 8, not 16.
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 2023년 8월 22일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

제품

질문:

2023년 8월 22일

댓글:

2023년 8월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by