2

我需要在没有 clear 命令的情况下使用 Matlab 释放内存(我在并行工具箱的 parfor 循环中,我不能调用 clear);例如,我读到了,而不是

clear v  

我可以设置

v=[]

问题是:使用'= []'我释放'v'的内存或者只是将v设置为一个空值并且之前的内存仍然被分配然后不可用?谢谢

4

2 回答 2

5

你没看错。这是一个演示:

我现在的计算机内存(在清除工作区后,但有一些剩菜和绘图):

>> memory
Maximum possible array:            54699 MB (5.736e+10 bytes) *
Memory available for all arrays:            54699 MB (5.736e+10 bytes) *
Memory used by MATLAB:             1003 MB (1.052e+09 bytes)
Physical Memory (RAM):            32695 MB (3.428e+10 bytes)

*  Limited by System Memory (physical + swap file) available.

分配十亿个元素数组并再次检查内存:

>> x = rand(1e6,1e3);
>> memory
Maximum possible array:            46934 MB (4.921e+10 bytes) *
Memory available for all arrays:            46934 MB (4.921e+10 bytes) *
Memory used by MATLAB:             8690 MB (9.113e+09 bytes)
Physical Memory (RAM):            32695 MB (3.428e+10 bytes)

*  Limited by System Memory (physical + swap file) available.

将变量设置为 []。大多数内存再次可用(注意有一点损失):

>> x = [];
>> memory
Maximum possible array:            54578 MB (5.723e+10 bytes) *
Memory available for all arrays:            54578 MB (5.723e+10 bytes) *
Memory used by MATLAB:             1061 MB (1.113e+09 bytes)
Physical Memory (RAM):            32695 MB (3.428e+10 bytes)

*  Limited by System Memory (physical + swap file) available.
于 2013-07-12T17:52:43.667 回答
1

借助函数“whos”很容易找到答案。例如,我创建了一个变量 v=1。

v=1;

键入“whos”,我们可以找到内存中的所有变量:

whos;
  Name      Size            Bytes  Class     Attributes

  v         1x1                 8  double   

我们可以在内存中找到变量 v。然后我尝试“删除” v:

v=[];

输入 'whos' 来检查它是否被删除:

 whos
  Name      Size            Bytes  Class     Attributes

  v         0x0                 0  double             

很明显,使用 'v=[];' 无法删除内存中的变量,它只是创建一个空变量。

clear;
whos;

什么都没有打印,内存中没有变量。

于 2013-07-13T02:59:02.823 回答