Mathematica caches all sorts of expressions that come up on the course of evaluation. Some but not all of these are cleared by ClearSystemCache
.
By your use of the expression Sqrt[1 + j^2] // N[#, d] &
you are performing exact symbolic computations and only then converting these to numeric equivalents. Mathematica will cache values in this process.
Put another ClearSystemCache[];
in your code before you check mem2
and you will see a very different number:
ClearSystemCache[];
d = 10^4;
mem1 = MemoryInUse[];
Sum[Sqrt[1 + j^2] // N[#, d] &, {j, 1, d}];
ClearSystemCache[];
mem2 = MemoryInUse[];
(mem2 - mem1)/d // N
9.9984
More suitable however itis to avoid exact symbolic calculation in the first place.
Here I replace 1
with an arbitrary precision one with d
digits of Precision
:
$HistoryLength = 0;
d = 10^4;10^4
one = SetPrecision[1, d];
mem1 = MemoryInUse[];MemoryInUse[]
out = Sum[Sqrt[one + j^2], {j, 1, d}];
mem2 = MemoryInUse[];MemoryInUse[]
(mem2 - mem1)/d // N
210000 26294360 26298432 0.00164072
This is sufficient to compute your result:
Precision[out]
10007.
In the code above simply allowing the result of d = 10^4
to print by omitting ;
uses some memory, resulting in the final delta being smaller. (Since mem1 = MemoryInUse[]
comes after this.) I suspect that this is because certain mechanisms are not fully primed until an expression is printed.
I also added $HistoryLength = 0
.