我需要用Matlab释放内存而不使用clear命令(我在并行工具箱的parfor循环中,我不能调用clear);我读到,例如,而非
clear v
我可以设置
v=[]
问题是:使用'=[]',我对'v'的内存进行了重新分配,还是直接将v设为空值,之前的内存仍然被分配,然后无法使用? 谢谢。
你的理解是正确的。 下面是一个示范。
我的电脑现在的内存(在清除工作区后,但有一些剩余的数据和绘图)。
>> 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.
在函数'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;
什么都没有打印,内存中没有变量。