3

I have a map called res_Map, containing a set of arrays of varying size. I want to find the total memory used to store res_Map.

As you can see below, it looks as if res_Map takes up almost no memory at all, whereas the individual elements in res_Map do.

res_1 = res_Map(1);
>> whos
  Name              Size             Bytes  Class             Attributes

  res_1           118x100            94400  double                      
  res_Map          11x1                112  containers.Map

Does anyone know how I can find the actual memory used to store res_Map? I can't find any info about this in the documentation.

Matthew Simoneau
  • 6,199
  • 6
  • 35
  • 46
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
  • 1
    I think that this related question might help: [http://stackoverflow.com/questions/2388409/how-can-i-tell-how-much-memory-a-handle-object-uses-in-matlab](http://stackoverflow.com/questions/2388409/how-can-i-tell-how-much-memory-a-handle-object-uses-in-matlab). – horchler Jun 03 '13 at 18:07
  • Thanks @horchler! It worked perfectly. Assuming that the comments to http://stackoverflow.com/a/2389080/2338750 is correct, saying that the conversion to struct does not take up much memory, this seems like a simple way of doing it. I think it's very strange that these sorts of tricks are necessary in Matlab. – Stewie Griffin Jun 03 '13 at 18:34

2 Answers2

4

The containers.Map object is a Matlab object like any other. Under the hood these are implemented as a Matlab structures with some additional access controls and function mappings.

You can force Matlab to show you the raw structure using the struct command. This throws a warning, as it is usually not recommended. However, the structure view of the class shows the complete contents, and is accurately reflected in a whos call.

Some example code is below:

%Initialize map and add some content
res_Map = containers.Map;
for ix = 1:1000
    res_Map(sprintf('%05d',ix)) = ix;
end

%Look at the memory used by the map
disp('Raw who:  always 112 Bytes for Map')
whos('res_Map')

%Force the map into a structure, and look at the contained memory
mapContents = struct(res_Map);
disp('Look at the size of the map contents, reflect true size')
whos('res_Map','mapContents')


%Add additional contents and check again.
for ix = 1001:2000
    res_Map(sprintf('%05d',ix)) = ix;
end
mapContents = struct(res_Map);
disp('Look at the size of the map contents, reflect true size')
whos('res_Map','mapContents')

The result of the above script (after removing the warning messages) is shown below:

Raw who:  always 112 Bytes for Map
Name            Size            Bytes  Class             Attributes
res_Map      1000x1               112  containers.Map

Look at the size of the map contents, reflect true size
Name                Size             Bytes  Class             Attributes
mapContents         1x1             243621  struct
res_Map          1000x1                112  containers.Map


Look at the size of the map contents, reflect true size
Name                Size             Bytes  Class             Attributes    
mapContents         1x1             485621  struct
res_Map          2000x1                112  containers.Map
Pursuit
  • 12,285
  • 1
  • 25
  • 41
1

There is a script to do this for any struct over at matlab central, which I believe will also work for a map.

To implement it yourself, you need to recurse over the content of the map and subsequently over all the fields in the structs or cells it may contain to determine the sizes.

Marc Claesen
  • 16,778
  • 6
  • 27
  • 62