Variable size: does it reflect real size after computation finishes?

3 views (last 30 days)
Hi all,
I have a program which produces a 40227 by 50 double matrix. I'd like to optimize storage so I want to check total memory usage. It is in structural variable "canti". If I run whos, MATLAB shows:
K>> whos
Name Size Bytes Class Attributes
INPname 1x60 120 char
a 1x1 8 double
ans 1x1 1 logical
bondL1 1x1 8 double
bondL2 1x1 8 double
bondR1 1x1 8 double
bondR2 1x1 8 double
canti 1x1 112 canbeam
However, structural variable "canti" contains a few large variables as follows:
K>> canti.dis
ans =
inpt: [40227x1 double]
val: [40227x50 double]
full: [40227x50 double]
trial: [40227x50 double]
Why would "canti" only uses 112 bytes of memory while it contains variables of 40227 by 50 matrix? According to my test, if I do:
>> a = rand(40227, 50);
>> whos
Name Size Bytes Class Attributes
a 40227x50 16090800 double
A 40227 by 50 double should cost 16090800 bytes in memory, which is 15.3454 MB. If MATLAB is not showing the real memory usage for some reason, then how can I check the memory usage during and after my program?
Many thanks!

Accepted Answer

Stephen23
Stephen23 on 26 May 2017
Edited: Stephen23 on 26 May 2017
A structure does not really contain any arrays, but is a list of links to other arrays in memory (some might call this an "an array of pointers"). As such, whos correctly returns the size of the structure itself, and not of the arrays that it has links to. See:
Unfortunately the standard whos cannot read the memory size (bytes) of arrays in a structure. You could try this FEX submission:

More Answers (1)

Walter Roberson
Walter Roberson on 26 May 2017
Notice you have
canti 1x1 112 canbeam
here, "canbeam" is the data class of the variable. It is not struct, not cell array, not numeric, but rather "canbeam", which is not a Mathwork-supplied data type. It must be an object oriented object from a class you have provided.
I suspect, from the size, that canbeam is a handle class. If so then because you can have multiple copies of the same handle, it would be incorrect to count the referenced memory against the handle: the 112 bytes is the size of the handle itself.
  1 Comment
Xh Du
Xh Du on 30 May 2017
Thank you, you were right, "canbeam" is an OO class. The size only reflects the class handle itself, not the content inside.

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!