필터 지우기
필터 지우기

Why cell arrays and structure consume extra memory?

조회 수: 17 (최근 30일)
Gautam
Gautam 2014년 8월 21일
편집: Guillaume 2014년 8월 22일
I observed today that a Matrix , a Cell Array and a Structure with same contents occupy different amount of memory in space and there's a huge difference between them. Why is that? Below is the snippet I executed:
a = {'hello'};
b = 'hello';
c = struct('string' , 'hello');
whos
Name Size Bytes Class Attributes
a 1x1 70 cell
b 1x5 10 char
c 1x1 134 struct
W

답변 (2개)

Iain
Iain 2014년 8월 21일
The memory for a structure needs to contain:
The size of the structure.
The names of the fields.
Something that says "Hey, I'm a structure"
The pointer to the structure
The memory location of each field
Each field needs to contain:
The size of the field
What datatype the field is
The contents of the field (the data, or another field)
A cell array needs to contain:
The pointer to the cell array
The size of the array (I'm 7d, and x by y by z by A by B by C by D).
Something that says "Hey, I'm a cell array"
Each cell needs to contain: The size of the cell contents (I'm 7d, and x by y by z by A by B by C by D).
The type of data in the cell
The contents of the cell (i.e. the data)
An array needs to contain:
The pointer to the array
The size of the array (I'm 7d, and x by y by z by A by B by C by D).
Something that says "Hey, I'm an X format number"
The data
  댓글 수: 3
Iain
Iain 2014년 8월 22일
편집: Iain 2014년 8월 22일
Typically, characters are 8 bit. You have 5 characters. You need 5 bytes for the data. It is consuming 10 bytes. ;)
Guillaume
Guillaume 2014년 8월 22일
Actually,
doc char
"Valid numeric values range from 0 to 65535". chars are 16-bit.

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


Guillaume
Guillaume 2014년 8월 21일
편집: Guillaume 2014년 8월 21일
Matrices will always use less memory as they only need as much memory as their content will take + memory for the size and type.
If you're familiar with C, a cell array can be considered as an array of pointers to arrays. Thus it needs memory for the main array + its size + its type, plus memory for the sub arrays + their size + their types.
A structure is like a cell array, but in addition it needs memory for the field names.
  댓글 수: 2
Gautam
Gautam 2014년 8월 21일
A matrix also has size and type attributes associated with them, but it isn't occupying any extra chunk of memory. Can you please give a detailed view of how much of memory is occupied by a particular attribute in cell arrays.
Guillaume
Guillaume 2014년 8월 22일
편집: Guillaume 2014년 8월 22일
I suggest you have a look at mxarray to learn more about memory storage of matlab data types.
I don't know exactly what whos takes into account. it does not appear to report the memory used to store the type and size of the main variable, but the reason cells and structs are bigger are as explained. Cells have an additional level of indirection, thus use more memory and structs also need some space to store the field names (and it looks like whos reports the space used by the pointer to the field name, but not the space used by the field name itself).
Also, note that you can't rely too much on the sizes reported as they won't be the same for 32-bit and 64-bit matlab. For example on my machine (Matlab 64 bit):
whos
Name Size Bytes Class Attributes
a 1x1 122 cell
b 1x5 10 char
c 1x1 186 struct

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

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by