Consider the following code snippet:
NestWhile[{#[[1]]+1,[email protected];ConstantArray[0,{100, 100}]}&,
{1}, (#[[1]] <= 10000) &, 2]
In theory, when evaluating, NestWhile
shall keep track of the last two results generated, thus consuming approximately 160kB of memory.
However, in reality, memory consumption will continue to grow at a pace of approximately 80MB/s before NestWhile
finishes calculation. Furthermore, regardless of the fourth parameter (as long as it is not 1), the speed of memory consumption is the same. These two phenomena indicate that Mathematica evaluates the expression and stored all results in memory until all computations are finished then picks out last two elements.
This behavior is rather disturbing for me: why save the previous evaluation results in memory if they are used in nowhere?
This behavior exists in v12.0 and v11.2
#Update
Received reply from technical support which said:
This behavior of storing all intermediate steps of a calculation is intended. Clearing intermediate information that has been stored can be done with the command
ClearSystemCache[]
However, after altering the code to:
NestWhile[(ClearSystemCache[]; {#[[1]] + 1, [email protected]; ConstantArray[0, {100, 100}]})&,
{1}, (#[[1]] <= 10000) &, 2]
still cannot stop fast increasing memory consumption during evaluation.
Furthermore, a new observation, when evaluating this piece of code, Mathematica will eventually consume 99% of memory, but will not try to use virutal memory. So theoretically this behavior will not influence the performance of Mathematica, however, if I open another program, Mathematica will encounter memory related issues sometimes and crash. But by all means, I think consuming all memory is not a good choice.