3

This question is inspired by the question on memory leaks in Mathematica due to internal caching of results of intermediate computations. All these things are undocumented but are important for anyone who runs memory-intensive computations.

When trying to understand the logic of the internal caching mechanism I found something interesting. Consider the following:

$HistoryLength = 0;
(*dummy command for loading of the Root package*)
Root[# &, 1];
d = 13;
f[z_, i_] := Sum[(2 Mod[Floor[(i - 1)/2^k], 2] - 1) z^(d - k), {k, 0, d}];
(memLog = Flatten[
     Table[Root[f[z, i], j]; {SessionTime[], MemoryInUse[]/1024.^2}, {j, 1, 
       d}, {i, 1, 2^d}], 1];) // Timing
pl1 = ListLinePlot[memLog, 
  FrameLabel -> {"SessionTime, sec", "MemoryInUse, Mb"}, PlotRange -> All, 
  Frame -> True, Axes -> False]
pl2 = ListLinePlot[memLog[[All, 2]], 
  FrameLabel -> {"Point", "MemoryInUse, Mb"}, PlotRange -> All, Frame -> True,
   Axes -> False]

In fresh kernel session the output on my machine (Mathematica 7.0.1 for Windows) is always as follows:

screenshot

Can anyone explain why there is a break of the curve near the point number 8400?

Community
  • 1
  • 1
Alexey Popkov
  • 9,355
  • 4
  • 42
  • 93
  • I think explaining the memory usage behavior of a complex piece of software without the source code at hand (and sometimes _with_ the source code right under your nose) is an oracle's job. From Wikipedia: _Oracles were thought to be portals through which the gods spoke directly to man._ – Dr. belisarius Jul 25 '11 at 02:32
  • 1
    @belisarius World and nature may also be called as "a complex piece of software without the source code at hand" in some sense but scientists explain its mystique and use its secrets for practical purposes. The key word here is *scientific approach* for investigation of black boxes. – Alexey Popkov Jul 25 '11 at 05:54
  • 1
    Nature is somehow _logical_, while software ... – Dr. belisarius Jul 25 '11 at 16:55
  • 1
    Moreover, what a physicist usually does is to create _models_. You know, a model is not an _explanation_, but just a rational approach useful to formulate _predictions_. A question like "why there is a break of the curve near the point number 8400" is more engineering than science. To be able to answer, you have to understand the internals of the system. Answers like the excellent one by Sjoerd below are models, and you can use them until a better model comes up ... – Dr. belisarius Jul 25 '11 at 21:23

1 Answers1

7

The point at which the change in slope occurs is no. 8192. This equals 2^13, which is the upper bound of the fastest of your Table indices, i. When mma arrives at this point it has calculated all possible f[z, i] values. Since this call is independent of j, the next value of j (2) will generate the same range (f[z, 1], ..., f[z, 8192]) again. I assume this is where internal caching kicks in and fewer additional memory is needed.

Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94