从 Matlab 的地图中释放内存

Freeing up memory from Matlab's Map

我遇到了一个问题,需要我以一种难以预测的方式跟踪大量数据。不过,我确实知道一些规则,这些规则允许我确定何时不再需要某些数据。

我正在考虑使用 containers.Map 来存储以高效方式生成的数据(因为我无法指定足够大的空矩阵来保存我的所有数据)。 然后我会 运行 我的代码,并且在每个步骤中,如果满足条件,使用 remove(M, keys).

清除一些存储的数据

但是我已经 运行 进行了一个非常基本的测试,当我删除元素时,分配给 Map 的内存似乎没有改变。这是我的测试 运行:

testmap = containers.Map([1,2,3],[11,22,33])
whos testmap % outputs a size of 3x1 as expected and an allocation of 8 Bytes
remove(testmap,[1]);
whos testmap % outputs a size of 2x1 as expected ... but still an allocation of 8 Bytes

我看对了吗?有没有另一种方法可以继续拥有一个可以根据内存使用情况动态调整的数据容器?

谢谢。

请注意,即使 映射也是 8 个字节,因此您的测试没有告诉您任何信息

testmap = containers.Map();
whos testmap

% Name         Size            Bytes  Class             Attributes
% testmap      0x1                 8  containers.Map              

容器本身占用的内存很少,我们要从this question带头确定关联地图数据的大小

testmap = containers.Map(1:10,1:10);
testmap_s = struct(testmap);
whos testmap_s

% Name           Size            Bytes  Class     Attributes
% testmap_s      1x1              3809  struct    

remove(testmap,{1,2,3,4,5,6,7,8});
testmap_s = struct(testmap);
whos testmap_s

% Name           Size            Bytes  Class     Attributes
% testmap_s      1x1              2017  struct  

所以是的,这应该适用于您的应用程序,数据将从映射中清除,并且应该在适当的时候“忘记”由 MATLAB 进行的任何额外内存管理。