【问题标题】:Increasing Stack Space增加堆栈空间
【发布时间】:2013-10-12 18:35:43
【问题描述】:

当我运行以下代码时:

(defun countdown (n)
  (if (>= n 0)
    (cons n (countdown (- n 1)))))

(countdown 100000)

我收到以下消息:

INFO: Control stack guard page unprotected
Control stack guard page temporarily disabled: proceed with caution

debugger invoked on a SB-KERNEL::CONTROL-STACK-EXHAUSTED in thread
#<THREAD "main thread" RUNNING {1002B03653}>:
  Control stack exhausted (no more space for function call frames).
This is probably due to heavily nested or infinitely recursive function
calls, or a tail call that SBCL cannot or has not optimized away.

PROCEED WITH CAUTION.

Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.

restarts (invokable by number or by possibly-abbreviated name):
  0: [ABORT] Exit debugger, returning to top level.

(SB-KERNEL::CONTROL-STACK-EXHAUSTED-ERROR)

如何增加堆栈空间?我不想更改上面的代码;我提供它纯粹是为了说明堆栈耗尽。

【问题讨论】:

  • 人们可能会质疑堆栈运行深度为 100K 的程序的设计。
  • @PaulNathan One 确实可能。这是出于基准测试的目的,而不是用于真正的程序。不幸的是,这个小空白中没有足够的空间来概述它......
  • @PaulNathan :如果您有兴趣,它与此处的气泡排序讨论有关:groups.google.com/d/msg/qilang/cUE5fj7XfjE/ueo3MkB7h30J
  • 似乎是合理的。 :-)

标签: common-lisp sbcl


【解决方案1】:

在命令行上使用--help 选项调用sbcl 会提供一些有用的常用选项,包括如何增加堆栈空间的选项。您想使用--control-stack-size 参数。

$ sbcl --help
Usage: sbcl [runtime-options] [toplevel-options] [user-options]
Common runtime options:
  --help                     Print this message and exit.
  --version                  Print version information and exit.
  --core <filename>          Use the specified core file instead of the default.
  --dynamic-space-size <MiB> Size of reserved dynamic space in megabytes.
  --control-stack-size <MiB> Size of reserved control stack in megabytes.
...

通过在此处指定更大的值,我们可以运行您的代码:

$ sbcl --control-stack-size 100000
* (defun countdown (n)
  (if (>= n 0)
    (cons n (countdown (- n 1)))))
;=> (100000 … 6 5 4 3 2 1 0)

【讨论】:

    【解决方案2】:

    对于手头的问题不完全及时,但对于任何想知道--control-stack-size的人来说:

    从SBCL manual 部分“3.3.1 运行时选项”中,--control-stack-size 的默认值为 2 兆字节。

    所以对于包含 100k 64 位整数 7 兆字节的列表,sbcl --control-stack-size 7 应该足够了!

    作为参考:

    CL-USER> (type-of 42)
    (INTEGER 0 4611686018427387903)
    CL-USER> (/ (* 64 100e3) (expt 2 20))
    6.1035156
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-09
      • 2012-04-07
      • 1970-01-01
      • 2010-11-24
      • 2011-11-20
      • 2015-11-22
      • 2011-03-25
      相关资源
      最近更新 更多