4

If I enter

Block[{$RecursionLimit = 70000}, x = x + 1]

I get

$RecursionLimit: Recursion depth of 70000 exceeded during evaluation of 1+x.

But at $RecursionLimit = 80000, Mathematica crashes (i.e. goes unresponsive for a little while and then clears all variables). Why is this? Is there some limiting factor that I can increase to go even further?

H.v.M.
  • 1,093
  • 5
  • 12
  • 4
    For me a more important question is why does this still crash the kernel? After 30 years, Wolfram still hasn't set up many of the basic catches that are required for a 'production system'. For me the 'crash limit' is >80,000. And the time it spends in unresponsiveness is quite long compared to the time when $RecursionLimit = 70000. MMA 11 and MMA 12. – berniethejet Aug 09 '20 at 18:57
  • For me, it's much lower, around $RecursionLimit = 16315 (varies). – Michael E2 Aug 09 '20 at 18:57
  • 3
    It crashes because the subroutine stack gets exhausted. – Daniel Lichtblau Aug 09 '20 at 19:03
  • I think that this depend on the amount of RAM of your system. – vi pa Aug 10 '20 at 07:47
  • I think that you can gain some information on the crash following the Mathematica kernel in the Task Manager until it crash. – vi pa Aug 10 '20 at 08:00

1 Answers1

11

This is by no means unique to Mathematica. It crashes due to stack overflow.

Python, and many other interpreters, will also crash with uncontrolled recursion. A major reason for $RecursionLimit (and Python's setrecursionlimit) is to avoid this.


Is there some limiting factor that I can increase to go even further?

Some operating systems let you increase a process's stack size. Perhaps on Linux it's ulimit -s. I don't have a Linux box here to test.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • ulimit -s will work for native code, but if the crash occurs in a part of the kernel which uses java or similar managed runtime the stack size limit would have to be set via some other means. – user130558 Aug 10 '20 at 03:52
  • @user130558 It does not use Java or "managed code" – Szabolcs Aug 10 '20 at 06:55
  • 1
    ulimit -s 65536 does prevent the crash with recursion limit of 70000 on Mathematica 12.0.0 for Linux x86 (64-bit). With ulimit -s 35000 or less (default being 8192 on Ubuntu) it crashes in libWolframEngine.so with a huge repeating backtrace, which is indeed a symptom of stack overflow due to recursion. – Ruslan Aug 10 '20 at 10:32