Filter löschen
Filter löschen

the Problem of Distribution memory of MATLAB struct?

5 Ansichten (letzte 30 Tage)
deng
deng am 4 Jun. 2016
Bearbeitet: James Tursa am 1 Jul. 2016
clear all
clc
testStruct.a=int8(10);
a=int8(10)
whos testStruct
whos a
the result is:
a =
10
Name Size Bytes Class Attributes
testStruct 1x1 125 struct
Name Size Bytes Class Attributes
a 1x1 1 int8
Variable a takes up 8 bytes, however, testStruct.a takes up 125 bytes。why? my email:dengshuaiqi@163.com,think you very much。
  1 Kommentar
James Tursa
James Tursa am 1 Jul. 2016
Bearbeitet: James Tursa am 1 Jul. 2016
The whos result is misleading for this comparison. For the following discussion the numbers cited assume a 64-bit system and that the overhead for a MATLAB variable is 124 bytes (this is approx).
First, every MATLAB variable has an overhead structure called an mxArray that contains information such as class, size, data pointers, sharing pointers, etc. So for variable "a" above, the data for "a" is only showing 1 byte, but the mxArray for this variable is taking up an additional 124 bytes of memory. So the total amount of memory actually used by variable "a" is 125 bytes. (actually probably more due to memory alignment effects)
For the struct testStruct, it too has this mxArray overhead. So there is 124 bytes right there. Then the data area of this struct is actually mxArray pointers, another 8 bytes. Then there is memory for the field name "a", so another 2 bytes minimum for that. Then the data itself is actually a MATLAB variable, so the overhead and data for that come to another 125 bytes. So the total for the struct testStruct is actually 124 + 8 + 2 + 125 = 259 bytes.
Bottom line is that you can't really compare the whos result of structs to numeric variables since whos does not tell you the whole memory story, it only reports on the data areas.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 4 Jun. 2016
The structure as a whole takes up 125 bytes, not just the .a field of the structure.
The structure includes the overhead to list all of the field names and the types and sizes of each of the fields.
  6 Kommentare
deng
deng am 6 Jun. 2016
I see.Think you very much.
Walter Roberson
Walter Roberson am 1 Jul. 2016
deng comments to my original Answer:
hello, problem of memmapfile function. think you very muchu. http://www.mathworks.com/matlabcentral/answers/293260-problem-of-memmapfile-function

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by