Please clear my doubt in memory calculation of cell Array

1 view (last 30 days)
Hi Team,
I've got a cell array of size 300. Each cell contains 6 fields(each field is of 8 byte) of class double .
My Query:
  1. I need to calculate the memory required for saving this cell array of size 300.
  2. If i reduce the field size from 6 to 3, Will my memory size be reduced to exactly half ?
My Understanding :
Memory Calculation of cell Array = (No.Of cells* 112 bytes per cell)+(field size *total no.of cell)
for field size 6 = (300*112)+(48*8(double)*300)= 148800 bytes
for field size 3 = (300*112)+(24*8*300) = 91200 bytes
I could see from my calculation,the memory size required for field size 3 is not reduced to exactly half the memory required for field size 6.
Please guide me if i'm wrong in my calculation . Also request you to help me out in solving my above two queries.
Regards,
Malini

Accepted Answer

Friedrich
Friedrich on 24 Sep 2012
Edited: Friedrich on 24 Sep 2012
Hi,
your calculation seems fine. You don't get half because of the cell header ("overhead") information which always needs 112bytes (it stores the information what the cell element contains (so data type, size etc)). This size is fixed and doesn't get smaller when your data gets smaller.
So you can see it like this:
a.) basic memory needed
300 cells => 300 * 112 bytes needed (assuming no cell is empty) (this is fix and won't change unless you use less cells).
b.) memory for the data
case I.) 48*8*300 = 115200 bytes
case II.) 24*8*300 = 57600 bytes
Total Memory = a.) + b.)
I hope this help

More Answers (1)

Jan
Jan on 24 Sep 2012
Edited: Jan on 24 Sep 2012
What exactly does "saving" mean here? And what do you count to the "used memory", e.g. how do you measure it?
The overhead of each variable is about 100 bytes, I assume this is your 112 bytes. Then you have 1 variable as cell and 300 variables as double vectors. So you have to start with 301 instead of 300 variables.
The cell contains pointers to the included variables, which have 32 or 64 bits according to the version of Matlab. Therefore you need additional 300*8 (or 4) bytes. But the cell must occupy some more memory, when you consider that it needs a name also and must be found by the interpreter. Therefore you need an additional string, which must be stored in a lookup-table. The memory manager of the operating system will use some overhead also to keep its list of reserved memory.
Since R2012a there are new method to handle a missing pre-allocation also. This will need additional memory also, but as far as I know this is not documented and depends on the JIT interpretation.
How do you want to count shared memory? Example:
C = cell(1, 100);
C(:) = {rand(1, 200)};
This creates the cell and headers for the 100 included variables, but the actual data are shared, in opposite to the following code:
C = cell(1, 100);
data = rand(1, 200);
for iC = 1:numel(C)
C{iC} = data;
end
I guess and hope, that the JIT is smart enough for some Matlab versions to created shared data copies. But for some versions this code creates deep data copies.
If the memory is fragmented, the small intermediate blocks between the real data cannot be used for anything else. Does this count as "used" memeory or not?
Finally there are so many unknowns and equivocal definitions, that it is hard to answer the question about memory "usage" exactly.

Community Treasure Hunt

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

Start Hunting!