What is the difference between the effect of clear and clearvars?

What is the difference between the effect of clear and clearvars?

1 comentario

I love to see, that somebody cares about the details of the clear command, because I've seen too many clear all without any use.

Iniciar sesión para comentar.

 Respuesta aceptada

Shashank Prasanna
Shashank Prasanna el 29 de En. de 2013
clearvars just clears variables you specify, in the way you specify.
clear is more powerful in the sense you can clear variables, variables, functions, compiled scripts, MEX-functions
Check our the doc for more info:

4 comentarios

Do you know if there is a difference in speed between clear and clearvars. From what you said, it seems clear does more so perhaps it would be slower than clearvars?
@Kien Pham: the question is rather moot: well-written code should very very rarely require clearing any variables:
Although popular with beginners, trying to micro-manage the MATLAB memory management often just impedes the JIT optimization.
@Stephen23 is there a way to provide a hint to the jit that a variable is no longer used? Or it already knows that ? Is this true for the GPU memory variables as well?
"is there a way to provide a hint to the jit that a variable is no longer used?"
Yes: CLEAR or CLEARVARS. But most of the time this is not required:
"Or it already knows that ?"
The MATLAB engine tracks the scoping of all variables and knows when they are no longer accessible.

Iniciar sesión para comentar.

Más respuestas (3)

@Kien Pham asked above whether there was a timing difference between clear and clearvars.
The answer astonished me: there is a big timing difference between the two!!
In the below code, I generate random assignment statements and write them to files. The files differ only in function name, and in whether they use "clear" or "clearvars" or have no clearing (keeping in mind that local variables are supposedly cleared when a function returns, so using a function is another form of clearing.)
Because the exact same assignment statements are used, there will be differences between the variations due to differences in how long the clearing takes; there should also be normal execution variances, so if the values are close then it is worth testing repeatedly to see if any particular relative order is accidental.
Note that it would normally be expected by programmers that the great majority of the execution time would be due to the assignment statements. Programmers tend to expect that clearing variables is fast. Mathworks does however warn that if you have a loop with a local variable that is being overwritten, then clearing the local variable is not good for performance
The results are:
  • not clearing is fastest
  • using "clear" is about 14 times slower -- too much difference for it to be due to chance
  • using "clearvars" is much slower. In this test, 25000 times slower!! And I could tell from my tests that the time taken is proportional to roughly the square of the number of variables !!
When I analyze the code for clearvars, I can see that in the case that no argument is provided, that it would execute
evalin('caller', 'clear -regexp ^.')
which tells us that the problem isn't exactly with clearvars, but rather that the speed problem is when using clear with -regexp .
The poor performance in this case is astounding, and I will create a support case about it.
N = 12000;
tn = tempname;
tn1 = tn + "clear.m";
tn2 = tn + "clearvars.m";
tn3 = tn + "noclear.m";
[folder, fn1, ext] = fileparts(tn1);
[~, fn2, ~] = fileparts(tn2);
[~, fn3, ~] = fileparts(tn3);
addpath(folder)
[fid1, msg] = fopen(tn1, 'w');
if fid1 < 0; error('Failed to open file "%s" because "%s"', tn1, msg); end
cleanme1 = onCleanup(@()delete(tn1));
[fid2, msg] = fopen(tn2, 'w');
if fid2 < 0; error('Failed to open file "%s" because "%s"', tn2, msg); end
cleanme2 = onCleanup(@()delete(tn2));
[fid3, msg] = fopen(tn3, 'w');
if fid3 < 0; error('Failed to open file "%s" because "%s"', tn3, msg); end
cleanme3 = onCleanup(@()delete(tn3));
fprintf(fid1, "function %s\n", fn1);
fprintf(fid2, "function %s\n", fn2);
fprintf(fid3, "function %s\n", fn3);
AL = 'A':'Z';
rn = AL(randi(length(AL), N, 62));
rv = compose(" = %.15g;", rand(N,1));
rl = rn + rv;
fprintf(fid1, "%s\n", rl);
fprintf(fid2, "%s\n", rl);
fprintf(fid3, "%s\n", rl);
fprintf(fid1, "\nclear\n");
fprintf(fid2, "\nclearvars\n");
fprintf(fid3, "\n");
fclose(fid1); fclose(fid2); fclose(fid3);
clear(fn1); clear(fn2); clear(fn3); %needed because m files were modified
dir(folder)
. hsperfdata_wguser tp06f0eb7b_8a00_4a71_9ec0_ee7b62775d90noclear.m .. jetty-0.0.0.0-8080-worker-webapp-_-any- worker.properties Editor_yhfth matlabpref ws_override.properties GDS-CACHE tp06f0eb7b_8a00_4a71_9ec0_ee7b62775d90clear.m hsperfdata_mcguser tp06f0eb7b_8a00_4a71_9ec0_ee7b62775d90clearvars.m
which(fn1)
/tmp/tp06f0eb7b_8a00_4a71_9ec0_ee7b62775d90clear.m
which(fn2)
/tmp/tp06f0eb7b_8a00_4a71_9ec0_ee7b62775d90clearvars.m
which(fn3)
/tmp/tp06f0eb7b_8a00_4a71_9ec0_ee7b62775d90noclear.m
fh1 = str2func(fn1);
fh2 = str2func(fn2);
fh3 = str2func(fn3);
fprintf('timing with clear\n');
timing with clear
timeit(fh1, 0)
ans = 0.0043
fprintf('timing with clearvars\n');
timing with clearvars
timeit(fh2, 0)
ans = 8.2146
fprintf('timing with no clear or clearvars\n');
timing with no clear or clearvars
timeit(fh3, 0)
ans = 2.9454e-04

5 comentarios

@Jan you will want to see this
Thanks, Walter. In fact, this is interesting.
Beside the runtime of evalin('caller', 'clear -regexp ^.'), this command might enable the usage of the lookup table of variables. The same effect happens for eval commands: Without these poking, the JIT can identify variables and inject a pointer to their data. After poking in the list of variables by dynamically creating of deleting variables, each access needs to locate the variables in the list, which takes time for searching.
This can happen also, if you change the type of a variable: x=1; ... x='a'; let the time of accessing a variable grow. I've tested this with R2009a and avoid this programming pattern. Maybe the JIT is smarter today.
The conclusions seem to be easy:
  • Avoid all forms of eval, even the evalin in clearvars.
  • Clearing variables is not useful except if they are such huge, that they fill the RAM.
Is this sufficient and exhaustive?
PS. It is interesting, what you have found in the temp folder. I still think that Matlab has enough power to break out of the virtual machine. At least the limitation of the run time blocks Spectre and Meltdown attacks.
@Walter Roberson Thank you for taking the time to compare the efficiacy between not using, using 'clear' and using 'clear vars'. I understand that using clear is not necessary usually but that doesn't really help me determine when to use and when not to use it. Is there a rule on when NOT to use clear?
Walter Roberson
Walter Roberson el 31 de Jul. de 2021
Editada: Walter Roberson el 1 de Ag. de 2021
Use clear or clearvars:
  • outside of any function, when you are at the command prompt and want to reset your MATLAB session because you are done with the previous task and want to start a new task
  • outside of any function, when you are at the command prompt, and you are working through debugging a script and you are worried that changes you made in the code are incompatible with values you already have in your workspace and your code does not have proper initialization
  • inside of a function when you have finished dealing with a variable and you do not have enough memory to store that variable and the next variable you need. If you are not short on free space, much of the time it is better to leave the variable in place.
  • Every once in a while, it can be easier to write code that depends upon whether a variable exists at all, and so sometimes you clear a variable so that the code recognizes that situation. However, most of the time this situation is better handled by setting a flag saying whether to do that processing or not
  • Sometimes you might be paying for memory use in a circumstance where you no longer need a large variable because you are finished with that phase of the program; in such a case it might be worth clearing the variable. However, typically it would be better to write such code as functions so that the unused variables are automatically removed.
Thanks for this detailed explanation. It would be useful to find such hints in doc clervars .

Iniciar sesión para comentar.

if you define a global variable, clearvars will remove it as well, however, somehow its value is still accessible in the memory and next time you define a global variable with the same name, the value of the previously removed variable will be assigned to the new one! Which is very strange!
global Test
Test = 10;
clearvars
if you check the workspace after above, you will see there is no variable called Test at this point, however:
global Test
display(Test)
Now Test has a value of 10 again! This strange behavior does not happen if you use clear all to remove the variables.
I am not sure if it is a bug or something designed, but clearvars did cause me problems and I found it the hard way!

4 comentarios

I understand that this caused you trouble, but I do not think it is a bug. I was particularly lookig for a way to remove a global variable in one function but still having access to it in other workspaces, so I am quite happy there is a difference between clear and clearvars.
Daniel
Daniel el 13 de Dic. de 2018
Editada: Walter Roberson el 30 de Jul. de 2021
you should use
clearvars -global
If you use clear, you can get the same result.
Because you need the (-)global flag to remove global variables. Or you can make your example an implicit complaint about the lack of alternative to global variables.

Iniciar sesión para comentar.

Fady Dawoud
Fady Dawoud el 28 de Sept. de 2021
Editada: Fady Dawoud el 28 de Sept. de 2021
I also would like to know the answer perhpas from a mathwork developer privy to the underlying code differences. I am encountering a scenario where if I use 'clearvars' from within a running script, memory is not released (as seen on task manager) but when I stop script and run 'clearvars' manually (using F9), magically memory is released. I tried the same with 'clear all' and in both cases (within running script vs manually) memory is released.
I know there are other ways to re-write code to be more memory efficient (most obviously using functions instead of scripts) but it would be helpful to know the difference in mechanics between 'clear all' and 'clearvars'.

4 comentarios

The task manager is not really useful to examine the memory usage dynamically. If memory is "released", it is not free for re-using immediately. It is only marked from the application as "not longer occupied". The operating system is responsible for overwriting the memory with zeros, before it is available for new allocations.
clear all removes loaded functions from the memory. When any function is used afterwards, Matlab has to reload it from the slow disk. This let the CPU idle, such that the OS has time for clearing the released RAM.
Maybe a pause() command after clearvars would change the situation.
Thanks Jan for taking the time to clarify. The reason I know memory is not released is that I am running a very long simulation and as I watch task manager memory creeping up , my station gets slower and eventually matlab either crashes or errors out saying out of memory.
Among the things I tried was putting a 'pause' before and after 'clearvars' but unfortunately this didn't help. appreciate any other thoughts.
the workaround for now is I do 'clearvars' , store all variables in workspace to disk, do 'clear all' , load workspace varialbes back. I do this with every scenario I have in the simulation. With this workaround, I see memory usage in task manager sharply dipping down after finishing one scenario and slowly climb up until the next 'clear all'
Thanks Walter for the suggestion. I also tried 'pack' but it didn't seem to make a difference if called from within running script or from command line. I also tried other clear('itemtype') with arguments functions, global and import with no luck.

Iniciar sesión para comentar.

Categorías

Más información sobre Environment and Settings en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 29 de En. de 2013

Comentada:

el 18 de Dic. de 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by