MemoryInUse and Print

• To: mathgroup at smc.vnet.net
• Subject: [mg99283] MemoryInUse and Print
• From: replicatorzed at gmail.com
• Date: Sat, 2 May 2009 05:55:46 -0400 (EDT)

```Can someone explain the following behaviour of Mathematica 7?

Starting with a fresh kernel, during a Do loop a constant amount of
memory is reported by MemoryInUse. This is completely ok.

------------[begin]--------
Do[Print[MemoryInUse[]],{5}]
10035976
10035976
10035976
10035976
10035976
------------[end]----------

With an other fresh kernel, one cell with 5 consecutive statements
will return five different values, increasing. Since no direct output
is returned, nothing is saved in Out, therefore I don't really
understand why the amount increases.

------------[begin]--------
Print[MemoryInUse[]];
Print[MemoryInUse[]];
Print[MemoryInUse[]];
Print[MemoryInUse[]];
Print[MemoryInUse[]];

10037304
10038112
10038752
10039384
10039920
------------[end]----------

Nevertheless if I limit the \$HistoryLength, the reported amount
decreases:

------------[begin]--------
\$HistoryLength = 0;
Print[MemoryInUse[]];
Print[MemoryInUse[]];
Print[MemoryInUse[]];
Print[MemoryInUse[]];
Print[MemoryInUse[]];

10037792
10037560
10037336
10037104
10036776
------------[end]----------

My questions therefore:
1. How can it be that the seemingly identical statements (5 x Print
[MemoryInUse[]];) give different results in case of the three
experiments?
2. How can I rely on Print[MemoryInUse[]]; in situations like:

------------[begin]--------
Print[MemoryInUse[]];
statement;
x = hugeValue;
Print[MemoryInUse[]];

10624000
14625472
------------[end]----------

if the report (i.e. Print[MemoryInUse[]];) itself takes some
*undefined* amount of memory? How can I get an indirect hint on the
memory usage of "statement" and "hugeValue"? Note that ByteCount can
not be used for several reasons.

3. Why does \$HistoryLength = 0; cause a decrease in used memory?

4. Is there a way (i.e. a certain \$HistoryLength value), when the
second or third experiment would report a constant amount of used
memory?