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.