2

我有一个名为的地图res_Map,其中包含一组不同大小的数组。我想找到用于存储的总内存res_Map

正如您在下面看到的,它看起来好像res_Map几乎不占用任何内存,而其中的单个元素res_Map确实如此。

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

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

有谁知道我如何找到用于存储的实际内存res_Map我在文档中找不到有关此的任何信息。

4

2 回答 2

3

containers.Map对象与任何其他对象一样是 Matlab 对象。在引擎盖下,这些被实现为带有一些附加访问控制和函数映射的 Matlab 结构。

您可以强制 Matlab 使用该struct命令向您显示原始结构。这会引发警告,因为通常不建议这样做。但是,类的结构视图显示了完整的内容,并准确地反映在whos调用中。

一些示例代码如下:

%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')

上述脚本的结果(删除警告消息后)如下所示:

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
于 2013-06-03T18:46:26.307 回答
1

structmatlab central有一个脚本可以为任何人执行此操作,我相信它也适用于地图。

要自己实现它,您需要递归地图的内容,然后递归它可能包含的structs 或cells 中的所有字段以确定大小。

于 2013-06-03T18:10:07.580 回答