构造单元变量

时间:2016-05-27 12:18:25

标签: matlab memory-leaks

编辑我怀疑下面问题中的RAM使用混乱是由于MATLAB"软链接"进程,如果y是N个字节,则执行x = y将产生大小为N个字节的x,但不使用额外的内存,因为这两个变量是相同的。但x=y+1当然需要2 * N内存,因为变量现在不同了。 我在这个假设中是否正确?

这是我编写的用于处理一些二进制数据的小函数。在我展示一些结果(变量大小)之后,我的问题将会出现。

function rawadc = SOexpand(rawdat)
% check for single vector vs. cellstruct
if ~iscell(rawdat),
    rawdat = mat2cell(rawdat,length(rawdat),1);
end
% now cycle thru length of cell var
rawadc = cell(length(rawdat),1);
for jj = 1:length(rawdat) % loop over frames
    tmpin = typecast(rawdat{jj},'uint16');
    tmp123 = bitand(tmpin ,hex2dec('FFF0')) ;
    % reshape so can set up the right order for inserting 4th words
    tmp123 = reshape(tmp123,3,length(tmpin)/3);
    tmp4  = bitshift( bitand(tmpin(1:3:end),hex2dec('000F')),12)...
        + bitshift( bitand(tmpin(2:3:end),hex2dec('000F')),8)...
        + bitshift( bitand(tmpin(3:3:end),hex2dec('000F')),4);
    % reassemble 
    tmpall = [tmp123;tmp4'];
    rawadc{jj} = reshape(tmpall,[numel(tmpall),1]);
end
end % of main

我的源数据是一个单元格变量:

>> whos('datfoo')
  Name           Size                 Bytes  Class    Attributes

  datfoo      1280x1             4026675200  cell 

如果我选择要处理的子集,

>> dt(1:100) = datfoo(1:100);
>> medsad = expandSad(dt);

然后构建一个非常大的单元格数组:

>> mot = medsad;
>> for jj = 1:6
mot = [mot;mot];
end
>> whos('mot')
  Name         Size                  Bytes  Class    Attributes

  mot       6400x1             26844262400  cell  

根据Windows任务管理器,这很好,我的RAM使用量很小,即可能增加半个Gb或更少。

但是,如果我通过函数运行整个源变量,

>> bigmot = SOexpand(datfoo);
>> whos('bigmot')
  Name           Size                 Bytes  Class    Attributes

  bigmot      1280x1             5368852480  cell 

该类是相同的,bigmot的字节大小是“{1}”的一小部分。 , RAM使用量增加了近5 Gb!如果我将bigmot保存到.mat文件并将其从工作区中删除,我将获得5Gb。从.mat文件重新加载会花费我相同的5Gb。所以问题是:MATLAB在这做什么?尽管报告了字节大小,为什么我的函数会创建一个耗费如此巨大内存块的大型单元变量? (为什么在控制台上基本上没有相同的操作呢?)

如果您想了解更多信息,请发表评论。 Windows7,MATLAB2015a,i3 quadcore。

0 个答案:

没有答案