MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Does MemoryConstrained count stack space?

  • To: mathgroup at
  • Subject: [mg117425] Re: Does MemoryConstrained count stack space?
  • From: Leonid Shifrin <lshifr at>
  • Date: Fri, 18 Mar 2011 05:59:57 -0500 (EST)


My impression of the memory consumption by the stack has been that it is
much smaller than memory typically used for computations. In other words,
you have
just a relatively small amount of memory allowed to be used by the stack
until the kernel

In[1]:= MaxMemoryUsed[]

Out[1]= 18009584

In[2]:= Quiet@Module[{f, n = 0}, f[x_] := (n += 1; f[x + 1]);
   TimeConstrained[Block[{$RecursionLimit = 65000}, f[0]], 0.5, n]];

In[3]:= MaxMemoryUsed[]

Out[3]= 29857584

It is about 10 Mb only, and on my system (AMD Phenom X6, Win7, M8.0, 8Gb
RAM) setting the
$RecursionLimit  to a slightly larger value results in a crash for this
particular function. Given that
generally the Mathematica's memory-constraining functions are working
approximately at best,
and given also that the stack space seems to often be only a small fraction
of the total memory
used in a computation, the memory-based method of avoiding crashes does not
seem promising
to me.

If it is the crashes that you want to prevent, I'd set a fixed finite
$RecursionLimit, or, even better,
look carefully at your functions to see why they need such a deep recursion
and whether you can
rewrite the code to use iterations. OTOH, if you want to prevent the large
memory consumption,
evaluation stack seems to not be the main contributing factor. If your goal
is to prevent excessive
swapping,  in this thread

a function is developed which you may find useful.


On Thu, Mar 17, 2011 at 2:34 PM, Alexey <lehin.p at> wrote:

> Hello,
> On the Documentation page for $RecursionLimit we read:
> "MemoryInUse and related functions do not count stack space. "
> And also:
> "On most computers, each level of recursion uses a certain amount of
> stack space. $RecursionLimit allows you to control the amount of stack
> space that Mathematica can use from within Mathematica. On some
> computer systems, your whole Mathematica session may crash if you
> allow it to use more stack space than the computer system allows. "
> Does MemoryConstrained count stack space? If not, is there another
> function that fulfills the following statement taken from the
> documentation page for MemoryConstrained:
> "MemoryConstrained generates an interrupt to stop the evaluation of
> expr if the amount of additional memory requested during the
> evaluation of expr exceeds b bytes."
> ?
> I mean a function that will count ALL additional memory requested
> during evaluation, including stack space.

  • Prev by Date: Re: Understanding Mathematica's evaluation procedure
  • Next by Date: Re: Changing default options of functions in a package when loading it
  • Previous by thread: Does MemoryConstrained count stack space?
  • Next by thread: Re: Does MemoryConstrained count stack space?