Skip to main content
added 631 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

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.

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 it to avoid exact symbolic calculation in the first place:

d = 10^4;
one = SetPrecision[1, d];
mem1 = MemoryInUse[];
Sum[Sqrt[one + j^2], {j, 1, d}];
mem2 = MemoryInUse[];
(mem2 - mem1)/d // N
2.0016

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 is 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
one = SetPrecision[1, d];
mem1 = MemoryInUse[]
out = Sum[Sqrt[one + j^2], {j, 1, d}];
mem2 = MemoryInUse[]
(mem2 - mem1)/d // N
10000

26294360

26298432

0.4072

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.

Post Undeleted by Mr.Wizard
added 837 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

Do you realizeMathematica caches all sorts of expressions that you are creating an output with 10,000 digits? Ofcome up on the course of evaluation. Some but not all of these are cleared by ClearSystemCache.

By your use of the expression takes some bytesSqrt[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 // ByteCountN
46729.9984

More suitable however it to avoid exact symbolic calculation in the first place:

d = 10^4;
one = SetPrecision[1, d];
mem1 = MemoryInUse[];
Sum[Sqrt[one + j^2], {j, 1, d}];
mem2 = MemoryInUse[];
(mem2 - mem1)/d // N
2.0016

Do you realize that you are creating an output with 10,000 digits? Of course the expression takes some bytes:

Sum[Sqrt[1 + j^2] // N[#, d] &, {j, 1, d}] // ByteCount
4672

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 it to avoid exact symbolic calculation in the first place:

d = 10^4;
one = SetPrecision[1, d];
mem1 = MemoryInUse[];
Sum[Sqrt[one + j^2], {j, 1, d}];
mem2 = MemoryInUse[];
(mem2 - mem1)/d // N
2.0016
Post Deleted by Mr.Wizard
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

Do you realize that you are creating an output with 10,000 digits? Of course the expression takes some bytes:

Sum[Sqrt[1 + j^2] // N[#, d] &, {j, 1, d}] // ByteCount
4672