While assigning a variable, is the variable cleared by default before the assignment?

8 views (last 30 days)
Hello all,
please clarify my doubt regarding variable assignment in MATLAB.
say
for iLoop=1:100%Line_1
output=someCalculation(iLoop);%Line_2
someOtherCalculation(output);%Line_3
...
...
end%Line_n
In line 2, we can see that output is written with value from someCalculation(). Is output, internally cleared before executing the next loop?
The background of my question is, one of our scripts (I realise scripts are bad. But legacy scripts), was creating a variable 'output' - (quite a huge variable - 1000s of MBytes) in every iteration.
In one of the iteration, there was memory error in Line 2. I have inserted a 'clear output' between line 1 and 2, and expect script to run without memory error.
Is my expectation correct?
In other words, while creating output for ith iteration, is the size of output from i-1 th iteration a bottleneck in terms of memory?
  1 Comment
Rik
Rik on 15 May 2019
Shouldn't you be able to check this by creating an array that should fill most of your memory and comparing the results of running it with and without a clear statement?

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 15 May 2019
Edited: Guillaume on 15 May 2019
Is output, internally cleared before executing the next loop?
No. A variable is only cleared if
  • you assign something else to it. The clearing occur at the point where the assignment occurs and only then.
  • it goes out of scope (e.g. a variable created inside a function is cleared when the function ends)
  • you explicitly clear it
Anything else would make no sense and break a lot of code.
I have inserted a 'clear output' between line 1 and 2, and expect script to run without memory error
If someCalculation does create a GB sized output, then yes it will make some difference. The previous content of output gets cleared when you assign the new result of someCalculation and only then. So, while someCalculation is running, with its own internal variable that takes GB of memory, you also have the previous content of output sill in memory. Rather than clear I'd recommend setting output to [].
while creating output for ith iteration, is the size of output from i-1 th iteration a bottleneck in terms of memory?
Only when someCalculation is being executed.
Of course, all this assumes that you haven't made any copy of output. Otherwise, these copies still take some memory.
  2 Comments
ES
ES on 16 May 2019
Hello Guillaume,
Thanks very much for the answer. My question is specifc to 'clearing occur at the point' you have mentioned in your answer.
Do you please know what does 'at the point' mean? Whether output is cleared, and then new value stored? Or even before assigning output, MATLAB decides there wont be space for output variable (because other variables (including output) are already occupying all the available memory?
Guillaume
Guillaume on 16 May 2019
In programming, there is the concept of sequence points. This gives you guarantee that something will happen before something else. It's not documented in matlab, but it would be difficult to write deterministic code if matlab didn't follow the same rules.
In the line
output = somefunction();
The execution of somefunction is a sequence point. It must occur before anything happens to output because that function may actually use the current state of output. Therefore, somefunction will be executed, possibly creating a huge variable, then and only then, the output of the function is assigned to output clearing the previous content.
That's the theory anyway. Now, with JIT compilation, the optimiser may see that somefunction is independent of output and that the output will be cleared and do so preemptively. I'm making suppositions here, as the JIT optimisations are not documented. In any case, it would never be something you could rely on as it would depend on many things completely outside your control.
So, if you want to be guaranted that you don't temporarily use twice as much memory, yes, you need to clear the variable before executing the above line. As I said, rather than clear, you'd be better off, making it empty:
output = [];
output = somefunction();

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2011b

Community Treasure Hunt

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

Start Hunting!