3

More of a blue skies question here - if I have some code that is like

A = [1,2,3,4,5,6]; %input data

B = sort(A); %step one

C = B(1,1) + 10; %step two

Is there a line of code I can use to remove "B" to save memory before doing something else with C?

user2587726
  • 169
  • 2
  • 11
  • What do you mean by remove `B`, you want to clear this variable ? But then how would it be possible to access a variable that does not exist ? If we take your example, you can write `C` like that: `C= min(A) + 10` so `B` is no more needed. – obchardon Jul 10 '19 at 15:27
  • The answer depends on the operation you apply to `B`. If it's indexing as in your example, this is answered [here](https://stackoverflow.com/questions/3627107/how-can-i-index-a-matlab-array-returned-by-a-function-without-first-assigning-it) – Luis Mendo Jul 10 '19 at 21:32

2 Answers2

1
clear B

This will remove the variable B from memory. See the documentation here for more info.

Silver
  • 269
  • 2
  • 10
1

There is no need to assign each result to a new variable. For example, you could write:

A = [1,2,3,4,5,6]; %input data
A = sort(A); %step one
A = A(1,1) + 10; %step two

Especially if A is large, it is much more efficient to write A = sort(A) than B = sort(A), because then sort can work in-place, avoiding the need to create a secondary array. The same is true for many other functions. Working in-place means that the cache can be used more effectively, speeding up operations. The reduced memory usage is also a plus for very large arrays, and in-place operations tend to avoid memory fragmentation.

In contrast, things like clear B tend to slow down the interpreter, as they make things more complicated for the JIT. Furthermore, as can be seen in the documentation,

On UNIX® systems, clear does not affect the amount of memory allocated to the MATLAB process.

That is, the variable is cleared from memory, but the memory itself is not returned to the system.


As an aside, as @obchardon said in a comment, your code can be further simplified by realizing that min does the same thing as keeping only the first value of the result of sort (but much more efficiently).


As an example, I've put three operations in a row that can work in-place, and used timeit to time the execution time of these two options: using a different variable every time and clearing them when no longer needed, or assigning into the same variable.

N = 1000;
A = rand(1,N);
disp(timeit(@()method1(A)))
disp(timeit(@()method2(A)))

function D = method1(A)
B = sort(A);
clear A
C = cumsum(B);
clear B
D = cumprod(C);
end

function A = method2(A)
A = sort(A);
A = cumsum(A);
A = cumprod(A);
end

Using MATLAB Online I see these values:

  • different variables + clear: 5.8806e-05 s
  • re-using same variable: 4.4185e-05 s

MATLAB Online is not the best way for timing tests, as so many other things happen on the server at the same time, but it gives a good indication. I've ran the test multiple times and seen similar values most of those times.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • I find when I use the same name for several steps it can become confusing for me, hence I'm wondering if I can use different names for each step then "delete", for want of a better word, these intermediate steps, such that I've just gone from start to finish. Is there a way of measuring the slow down things like clear B would do? – user2587726 Jul 11 '19 at 16:04
  • @user2587726: See the test I added to the bottom of the answer. In short, yes, you *can* use different variables and `clear` them when no longer needed. But it is less efficient, and I recommend that you learn to reason differently about your code. – Cris Luengo Jul 11 '19 at 16:24