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

1 次查看(过去 30 天)
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!

采纳的回答

Stephen23
Stephen23 2017-5-26
编辑:Stephen23 2017-5-26
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:

更多回答(1 个)

Walter Roberson
Walter Roberson 2017-5-26
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 个评论
Xh Du
Xh Du 2017-5-30
Thank you, you were right, "canbeam" is an OO class. The size only reflects the class handle itself, not the content inside.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Variables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by