Pergunta

I need to free memory with Matlab without clear command (I'm inside a parfor loop of parallel toolbox and I can't call clear); I read that,for example, instead of

clear v  

I can set

v=[]

the question is: with '= []' I deallocate the memory of 'v' or just set v to an empty value and the previous memory is still allocated and then unusable? thanks

Foi útil?

Solução

You read correctly. Here's a demonstration:

My computer's memory right now (after clearing the workspace, but with some leftovers and plots in place):

>> 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.

Allocate a billion element array and check memory again:

>> 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.

Set the variable to []. Most memory is again available (note a small loss):

>> 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.

Outras dicas

It's easy to find the answer with the help of function 'whos'.For example,I create a variable v=1.

v=1;

type 'whos',we could find all variables in memory:

whos;
  Name      Size            Bytes  Class     Attributes

  v         1x1                 8  double   

we could find the variable v in memory. Then I try to 'delete' v:

v=[];

type 'whos' to check whether it is delete or not:

 whos
  Name      Size            Bytes  Class     Attributes

  v         0x0                 0  double             

It is very clear that using 'v=[];' could not delete a variable in memory,it's just create an empty variable instead.

clear;
whos;

Nothing is printed,there is no variable in memory.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top